Advertisement
roman_gemini

Function 01

Jun 20th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; function that iterates over collection
  2. ; and multiplies elements by 2 or 3 if odd or even
  3. ; element
  4. (defn func1 [arr]
  5.   (for [x arr] (* x (if (odd? x) 2 3))))
  6.  
  7. ; function that iterates over collection and
  8. ; multiplies or divides elements by 2 if it is
  9. ; odd or even accordingly
  10. (defn func2 [arr]
  11.   (for [x arr] ((if (odd? x) * /) x 2)))
  12.  
  13. ; generating range
  14. (def a (range 20))
  15.  
  16. ; curried function for subtract
  17. (defn sub [a]
  18.   (fn [n] (- n a)))
  19.  
  20. ; fibonacci function that returns fibonacci number by
  21. ; index in fibonacci requence
  22. (defn fib [n]
  23.   (case n 0 0 1 1 2 1
  24.     (+ (fib (dec n)) (fib ((sub 2) n)))))
  25.  
  26. ; function for factorial
  27. (defn ! [n]
  28.   (case n 0 1 1 1 (* n (! (dec n)))))
  29.  
  30. (print (map ! a))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement