view 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
line wrap: on
line source

module AllEven where
    allEven :: [Integer] -> [Integer]
    allEven [] = []
    -- Note: "h:allEven t" doesn't parse the same as some other languages. We're not invoking anything on "h".
    -- It is literally "h" joined to the list made by "allEven t"
    allEven (h:t) = if even h then h:allEven t else allEven t

    allEvenGuard :: [Integer] -> [Integer]
    allEvenGuard [] = []
    -- 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"
    allEvenGuard (h:t)
        | even h = h:allEven t
        | otherwise = allEven t
    
    allEvenComp :: [Integer] -> [Integer]
    allEvenComp lst = [x | x <- lst, even x]