Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ! Write out the definition of init
  2.  
  3. init [] = []
  4. init [x] = []
  5. init (x:xs) = x : init xs
  6.  
  7. ! Write out the definition of fold
  8.  
  9. foldr cons nil [] = nil
  10. foldr cons nil (x:xs) = cons x (foldr cons nil xs)
  11.  
  12. ! therefore nil = []
  13.  
  14. foldr cons [] [] = []
  15. foldr cons [] (x:xs) = cons x (foldr cons [] xs)
  16.  
  17. ! case analysis on xs in order to get three equations (like init has)
  18.  
  19. foldr cons [] [] = []
  20. foldr cons [] [x] = cons x []
  21. foldr cons [] (x:y:ys) = cons x (cons y (foldr cons [] ys))
  22.  
  23. ! cons x [] = []
  24.  
  25. foldr cons [] [] = []
  26. foldr cons [] [x] = []
  27. foldr cons [] (x:y:ys) = cons x (cons y (foldr cons [] ys))
  28.  
  29. ! cons x (y:ys) = x : y : ys
  30.  
  31. init = foldr cons [] where
  32.  cons x [] = []
  33.  cons x (y:ys) = x : y : ys
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement