Upcoming SlideShare
×

# Recursion and Functional Programming

662

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
662
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
10
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Recursion and Functional Programming

1. 1. Recursion - Secret sauce of Functional Programming
2. 2. Imperative to Functional <ul><li>Immutability </li><ul><li>Intermediate state? </li></ul><li>No Looping constructs </li><ul><li>Iteration? </li></ul></ul>
3. 3. Scheme (dialect of Lisp) <ul><li>atom </li><ul><li>42, foo </li></ul><li>list </li><ul><li>(foo bar)
4. 4. (42 (foo bar)) </li></ul><li>s-expressions – atom or list </li><ul><li>42
5. 5. (foo bar)
6. 6. (42 (foo bar)) </li></ul></ul>
7. 7. car, cdr <ul><li>lat
8. 8. ( foo bar baz )
9. 9. (car lat)
10. 10. foo
11. 11. (cdr lat)
12. 12. ( bar baz )
13. 13. (car (cdr lat))
14. 14. bar </li></ul>
15. 15. cons <ul><li>(cons baz ())
16. 16. ( baz )
17. 17. (cons bar (baz))
18. 18. ( bar baz )
19. 19. (cons foo ( bar baz ) )
20. 20. ( foo bar baz ) </li></ul>
21. 21. Primitive functions <ul><li>null? </li><ul><li>(null? ( quote () ) is #t
22. 22. (null? foo ) is #f
23. 23. (null? ( foo bar )) is #f </li></ul><li>atom? </li><ul><li>(atom? foo ) is #t
24. 24. (atom? ( foo bar )) is #f
25. 25. (atom? ( quote () ) is #f </li></ul><li>eq? </li><ul><li>(eq? foo foo ) is #t
26. 26. (eq? foo bar ) is #f </li></ul></ul>
27. 27. <ul><li>(member? foo ( quote () )) is #f
28. 28. (member? oops ( foo bar baz )) is #f
29. 29. (member? baz ( foo bar baz )) is #t </li></ul><ul><ul><li>foo == baz is #f
30. 30. (member? baz (bar baz))
31. 31. bar == baz is #f
32. 32. (member? baz (baz))
33. 33. baz == baz is #t </li></ul></ul>member?
34. 34. member? ( define member? ( lambda (a lat) ( cond (( null? lat) #f) (else (or ( eq? ( car lat) a) (member? a ( cdr lat)))) )))
35. 35. remove <ul><li>(remove bar (foo bar baz))
36. 36. (foo baz) </li></ul>( define remove ( lambda (a lat) ( cond (( null? lat) (quote ())) (( eq? ( car lat) a) ( cdr lat)) (else ( cons ( car lat) (remove a ( cdr lat)))) )))
37. 37. insertR <ul><li>(insertR baz bar (foo bar))
38. 38. (foo bar baz) </li></ul>( define insertR ( lambda (new old lat) ( cond (( null? lat) (quote ())) (( eq? ( car lat) old) ( cons old ( cons new ( cdr lat)))) (else ( cons ( car lat) (insertR new old ( cdr lat)))) )))
39. 39. insertL <ul><li>(insertL baz bar (foo bar))
40. 40. (foo baz bar) </li></ul>( define insertL ( lambda (new old lat) ( cond (( null? lat) (quote ())) (( eq? ( car lat) old) ( cons new ( cons old ( cdr lat)))) (else ( cons ( car lat) (insertR new old ( cdr lat)))) )))
41. 41. insert ( define insert ( lambda seq ( lambda (new old lat) ( cond (( null? lat) (quote ())) (( eq? ( car lat) old) ( seq new old lat)) (else ( cons ( car lat) (insertR new old ( cdr lat)))) ))))
42. 42. insertR, insertL using HOF ( define seqR ( lambda new old lat ( cons old ( cons new ( cdr lat))) )) ( define seqL ( lambda new old lat ( cons new ( cons old ( cdr lat))) )) ( define insertR (insert seqR)) ( define insertL (insert seqR))
44. 44. (add1 41) = 42
45. 45. sub1
46. 46. (sub1 43) = 42
47. 47. zero?
48. 48. (zero? 0) is #t
49. 49. (zero? 42) is #f </li></ul>
50. 50. Addition + 5 + 3 = 1 + (5 + 2) = 1 + 1 + (5 + 1) = 1 + 1 + 1 + (5 + 0) = 1 + 1 + 1 + 5 = 1 + 1 + 6 = 1 + 7 = 8
51. 51. Addition + ( define + ( lambda (n m) ( cond (( zero? m) n) (else ( add1 (+ n (sub1 m)))) ))))
52. 52. Multiplication X 5 * 3 = 5 + (5 * 2) = 5 + 5 + (5 * 1) = 5 + 5 + 5 + (5 * 0) = 5 + 5 + 5 + 0 = 5 + 5 + 5 = 5 + 10 = 15
53. 53. Multiplication X ( define x ( lambda (n m) ( cond (( zero? m) 0) (else (+ n (x n (sub1 m)))) ))))
54. 54. Exponent ^ 5 ^ 3 = 5 x (5 ^ 2) = 5 x 5 x (5 ^ 1) = 5 x 5 x 5 x (5 ^ 0) = 5 x 5 x 5 x 1 = 5 x 5 x 5 = 5 x 25 = 125
55. 55. Exponent ^ ( define ^ ( lambda (n m) ( cond (( zero? m) 1) (else (x n (^ n (sub1 m)))) ))))
56. 56. Tuple addition <ul>(addtup (1 2 3 4 5)) = 1 + (addtup (2 3 4 5)) = 1 + (2 + (addtup (3 4 5))) = 1 + (2 + (3 + (addtup (4 5)))) = 1 + (2 + (3 + (4 + (addtup (5))))) = 1 + (2 + (3 + (4 + (5 + (addtup ()))))) = 1 + (2 + (3 + (4 + (5 + 0)))) = 1 + (2 + (3 + (4 + 5))) = 1 + (2 + (3 + 9)) = 1 + (2 + 12) = 1 + 14 = 15 </ul>
57. 57. Tuple addition ( define addtup ( lambda (tup) ( cond (( null? tup) 0) (else (+ ( car tup) (addtup ( cdr tup)))) ))))