betoesquivel

learning list comprehensions in haskell

Oct 19th, 2014
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.Environment
  2.  
  3. fac 0 = 1
  4. fac n = n * fac (n-1)
  5.  
  6. -- list comprehensions
  7. -- 2 lists
  8. nouns = [ "hobo", "frog", "pope"]
  9. adjectives = [ "lazy", "grouchy", "scheming"]
  10. listComprehensions2lists = [adjective ++ " " ++ noun | adjective <- adjectives, noun <- nouns]
  11.  
  12. -- length function
  13. length' xs = sum [1 | _ <- xs]
  14.  
  15. -- remove lowercase letters
  16. justUpper xs  = [ c | c <- xs, c `elem` ['A'..'Z']]
  17.  
  18. -- I can do nested list comprehensions
  19. -- just put what you want to do with the nested list inside the function part
  20. -- then put outside from where you are drawing the nested lists...
  21. -- remember you can span the comprehensions across multiple lines for it to be cleaner...
  22. -- so please do that :)
  23.  
  24. main = print (justUpper "hOLaCacAAAA")
Add Comment
Please, Sign In to add comment