annotate 7-Haskell/all_even.hs @ 87:2b5341fc4555

Add Haskell Day 1 code and notes
author IBBoard <dev@ibboard.co.uk>
date Sat, 15 Jun 2019 17:31:22 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
87
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 module AllEven where
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 allEven :: [Integer] -> [Integer]
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 allEven [] = []
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 -- Note: "h:allEven t" doesn't parse the same as some other languages. We're not invoking anything on "h".
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 -- It is literally "h" joined to the list made by "allEven t"
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 allEven (h:t) = if even h then h:allEven t else allEven t
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 allEvenGuard :: [Integer] -> [Integer]
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 allEvenGuard [] = []
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 -- We can't include the empty list case because decomposing an empty list with "h:t" gives the exception "Non-exhaustive patterns in h : t"
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 allEvenGuard (h:t)
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 | even h = h:allEven t
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 | otherwise = allEven t
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 allEvenComp :: [Integer] -> [Integer]
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 allEvenComp lst = [x | x <- lst, even x]