5 kyu

Calculating with Functions

35,048 of 86,179BattleRattle

Description:

This time we want to write calculations using functions and get the results. Let's have a look at some examples:

seven(times(five())); // must return 35
four(plus(nine())); // must return 13
eight(minus(three())); // must return 5
six(dividedBy(two())); // must return 3
seven $ times $ five   ->  35 :: Int
four $ plus $ nine     ->  13 :: Int
eight $ minus $ three  ->   5 :: Int
six $ dividedBy $ two  ->   3 :: Int
seven(times(five)) # must return 35
four(plus(nine)) # must return 13
eight(minus(three)) # must return 5
six(divided_by(two)) # must return 3
seven(times(five())) # must return 35
four(plus(nine())) # must return 13
eight(minus(three())) # must return 5
six(divided_by(two())) # must return 3
seven multiplied-by five   ! must evaluate to 35
four plus nine             ! must evaluate to 13
eight minus three          ! must evaluate to 5
six divided-by two         ! must evaluate to 3
seven(times(five())); // must return 35
four(plus(nine())); // must return 13
eight(minus(three())); // must return 5
six(divided_by(two())); // must return 3
seven @@ times      @@ five  (* must return 35 *)
four  @@ plus       @@ nine  (* must return 13 *)
eight @@ minus      @@ three (* must return  5 *)
six   @@ divided_by @@ two   (* must return  3 *)

Requirements:

  • There must be a function for each number from 0 ("zero") to 9 ("nine")
  • There must be a function for each of the following mathematical operations: plus, minus, times, divided_by
  • Each calculation consist of exactly one operation and two numbers
  • The most outer function represents the left operand, the most inner function represents the right operand
  • Division should be integer division. For example, this should return 2, not 2.666666...:
eight(dividedBy(three()));
eight $ dividedBy $ three
eight(divided_by(three))
eight(divided_by(three()))
eight divided-by three
eight(divided_by(three()));
eight @@ divided_by @@ three
Functional Programming

Stats:

CreatedOct 17, 2013
PublishedOct 17, 2013
Warriors Trained205243
Total Skips39784
Total Code Submissions304254
Total Times Completed86179
JavaScript Completions47871
Ruby Completions3219
Python Completions35048
Haskell Completions261
Factor Completions10
C++ Completions415
OCaml Completions12
Total Stars6298
% of votes with a positive feedback rating91% of 7244
Total "Very Satisfied" Votes6138
Total "Somewhat Satisfied" Votes898
Total "Not Satisfied" Votes208
Ad
Contributors
  • BattleRattle Avatar
  • jhoffner Avatar
  • GiacomoSorbi Avatar
  • JohanWiltink Avatar
  • Voile Avatar
  • monadius Avatar
  • hobovsky Avatar
  • RobsonMoon Avatar
  • trashy_incel Avatar
  • MikChan Avatar
  • ejini战神 Avatar
  • Kacarott Avatar
  • Just4FunCoder Avatar
  • o2001 Avatar
  • saudiGuy Avatar
Ad