lazy sequences

Click here to load reader

Post on 29-Jun-2015




0 download

Embed Size (px)


Lazy sequences, examples in Clojure Functional Programming


  • 1. Lazy Sequences

2. What is that? An expressions evaluation is postponed until it is actually needed Can build complex sequences and pay only for the elements you actually need. Lazy techniques imply pure functions. 3. When to be lazy? When producing large, infinite or variablesized sequences. 4. Fibonacci 5. Fibonacci Definiton Base : F0 = 0 ; F1 = 1 Induction : For n > 1 , Fn = Fn-1 + Fn-2 6. Non Lazy 7. Lazy 8. Lazy Even better 9. Lazy There is no such thing as a free lunch. But with lazy sequences, you can have an infinite menu and pay only for the menu items you are eating at a given moment. 10. PseudoCode a = fibo[1000000] if true return a else return 11. PseudoCode So, if it is false, why do I need to calculate fibo[1000000] , if I wont use it? Be Lazy 12. Some more code (def lots-o-fibs (take 1000000000 (fibo))) This will take a LOT of time, wont it? (nth lots-o-fibs 100) 13. [email protected]