Skip to content

Instantly share code, notes, and snippets.

@jamescrowley
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamescrowley/10010374 to your computer and use it in GitHub Desktop.
Save jamescrowley/10010374 to your computer and use it in GitHub Desktop.
Haskell - Seven languages in Seven Weeks
-- Question 1
allEven :: [Integer] -> [Integer]
allEven [] = []
allEven (h:t) = if even h then h:allEven t else allEven t
allEven2 :: [Integer] -> [Integer]
allEven2 list = [a | a <- list, even a]
-- Question 2
reverse2 [] = []
reverse2 (h:t) = (reverse t)
reverse3 list = rev list []
where
rev [] y = y
rev (x:xs) y = rev xs (x:y)
-- Question 3
let allColours = [(a,b) | a <- colours, b <- colours, a < b]
where colours = ["black","white","blue","yellow","red"]
-- Question 4
let multiplicationTable = [(a,b,a * b) | a <- [1..12], b <- [1..12] ]
-- Question 5
let colours = ["red", "green", "blue"]
[(alabama, mississippi, georgia, tennessee, florida) |
alabama <- colours,
mississippi <- colours,
georgia <- colours,
tennessee <- colours,
florida <- colours,
mississippi /= tennessee,
mississippi /= alabama,
alabama /= tennessee,
alabama /= georgia,
alabama /= florida,
georgia /= florida,
georgia /= tennessee]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment