Guest User

Untitled

a guest
Jan 5th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.49 KB | None | 0 0
  1. ; Lecture 7 Notes
  2. ; Functional Abstraction
  3. ;
  4. ; Functions are values.
  5. ;
  6. ; Set comprehensions
  7. ; S = {0, 1, ..., 9}
  8. ; T = {i^2 | i #epsilon S}
  9. ;
  10. ; In Haskell:
  11. ; s = [0..9]
  12. ; t = [i * i | i <- s]
  13. ;
  14. ; sqr x = x * x
  15. ;
  16. ; map f [] = []
  17. ; map f (x:xs)
  18. ;  = f x : map f xs
  19. ;
  20. ; t = map sqr [0..9]
  21. ;
  22. ; Intermediate Student with Lambda
  23. ;
  24.  
  25. ; map: (x -> y) (listof x) -> (listof y)
  26. (define (map_function f lst)
  27.   (cond
  28.     [(empty? lst) empty]
  29.     [else
  30.      (cons (f (first lst))
  31.            (map_function f (rest lst)))]))
  32.  
  33. (map_function sqr (list 0 1 2))
  34.  
  35. ; S = {0, 1, ..., 9}
  36. ; T = {i | i #epsilon S, i even}
  37. ;
  38. ; S = [0..9]
  39. ; t = [i | i <- s, even i]
  40. ;
  41. ; filter p [] = []
  42. ; filter p (x:xs)
  43. ;    | p x
  44. ;      = x : filter p xs
  45. ;    | otherwise
  46. ;      = filter p xs
  47. ;
  48. ; t = filter even [0..9]
  49.  
  50. ; filter: (x -> boolean) (listof x) -> (listof x)
  51. (define (filter p lst)
  52.   (cond
  53.     [(empty? lst) empty]
  54.     [(p (first lst))
  55.      (cons (first lst) (filter p (rest lst)))]
  56.     [else
  57.      (filter p (rest lst))]))
  58.  
  59. ; Funcitons are values.
  60. ; (3 + 4) + 5
  61. ;
  62. ; #lambda x . x^2
  63. ;
  64. ; sqr x = x * x
  65. ;
  66. ; sqr = \x -> x * x
  67.  
  68. (define (sqr x)
  69.   (* x x))
  70.  
  71. (define sqr
  72.   (lambda (x)
  73.     (* x x)))
  74.  
  75. ; A definition is a name-value binding.
  76.  
  77. (sqr 4)
  78. => ((lambda (x) (* x x)) 4)
  79. => (* 4 4)
  80. => 16
  81.  
  82. ; New grammar rules:
  83. ; expr = (expr expr expr ...)
  84. ;      | (lambda (id id ...) expr)
  85. ;
  86. ; New reduction rules:
  87.  
  88. (define (f x1 ... xn) exp)
  89. => (define f (lambda (x1 ... xn) exp))
  90.  
  91. ((lambda (x1 .. xn) exp) v1 ... vn)
  92. => modexp
  93.  
  94. ; where modexp is exp wiht vi substituted for xi.
  95. ;
  96. ; Abstracting structural recursion on lists
  97. ;
  98. ; f [] = ?
  99. ; f (x:xs) = ? x ? f xs
  100. ;
  101. ; c is a combine function, b is a base value, and [] is a list
  102. ; foldr c b [] = b
  103. ; foldr c b (x:xs)
  104. ;    = c x (foldr c b xs)
  105.  
  106. (define (foldr c b lst)
  107.   (cond
  108.     [(empty? lst) b]
  109.     [else
  110.      (c (first lst)
  111.         (coldr c b (rest lst)))]))
  112.  
  113. (foldr cons empty lst)
  114.  
  115. ; Contract?
  116. ; foldr: (x y -> y) y (listof x) -> y
  117.  
  118. (foldr + 0 lst)
  119.  
  120. (define (make-adder x)
  121.   (lambda (y)
  122.     (+ x y)))
  123.  
  124. (define make-adder
  125.   (lambda (x)
  126.     (lambda (y)
  127.       (+ x y))))
  128.  
  129. ; Contract?
  130. ; make-adder: number -> (number -> number)
  131. ;
  132. ; Remember that the contract for + is
  133. ; +: number number -> number
  134.  
  135. (make-adder 3)
  136. => ((lambda (x) (lambda (y) (+ x y))) 3)
  137. => (lambda (y) (+ 3 y))
  138.  
  139. ; plus :: Num a => a -> (a -> a)
  140. ; plus x y = x + y
  141. ;
  142. ; sumlist xs = foldr (+) 0 xs
  143. ; sumlist = foldr (+) 0
Add Comment
Please, Sign In to add comment