betoesquivel icon

learning list comprehensions in haskell

betoesquivel | PRO | 10/19/14 04:07:12 PM UTC | 0 ⭐ | 451 👁️ | Never ⏰ | []
Haskell |

750 B

|

None

|

0 👍

/

0 👎

import System.Environment
 
fac 0 = 1
fac n = n * fac (n-1)
 
-- list comprehensions
-- 2 lists
nouns = [ "hobo", "frog", "pope"]
adjectives = [ "lazy", "grouchy", "scheming"]
listComprehensions2lists = [adjective ++ " " ++ noun | adjective <- adjectives, noun <- nouns]
 
-- length function
length' xs = sum [1 | _ <- xs]
 
-- remove lowercase letters
justUpper xs  = [ c | c <- xs, c `elem` ['A'..'Z']]
 
-- I can do nested list comprehensions
-- just put what you want to do with the nested list inside the function part
-- then put outside from where you are drawing the nested lists...
-- remember you can span the comprehensions across multiple lines for it to be cleaner... 
-- so please do that :)
 
main = print (justUpper "hOLaCacAAAA")

Comments