Skip to content

Latest commit

 

History

History
34 lines (23 loc) · 1.11 KB

File metadata and controls

34 lines (23 loc) · 1.11 KB

First Class functions

Idempot - given the same input you get the same output

####Note:: Write an expression to add up the numbers from 1 to 10 and return the overall total.

(+ 1 2 3 4 5 6 7 8 9 10)

####Note:: Create an expression to do the same calculation, but without having to write all the numbers. Hint: consider the functions called range and reduce.

The range function generates a sequence of numbers and when given arguments it does so from a specific range. The second number is exclusive, so for 1 to 10 the second argument should be 11.

(range 1 11)

Unfortunately we cant just add the result of a range, because it returns a lazy sequence So (range) by itself will create an error

(+ 1 (range 1 11))

Using a function called reduce we can calculate a single total value from all the numbers in the collection.

The reduce function take 2 arguments, the first is the function to apply to a data structure, the second is the data structure.

(reduce + (range 1 11))

(reduce + (1 2 3 4 5 6 7 8 9 10))