You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.5 KiB

4 years ago
module DayOne where
-- How many different ways can you find to write allEven?
allEven :: [Integer] -> [Integer]
allEven [] = []
allEven (h:t) = if even h then h:allEven t else allEven t
-- >>> allEven [1, 2, 3, 4, 5, 6]
-- [2,4,6]
--
allEven2 :: [Integer] -> [Integer]
allEven2 xs = [x | x <- xs, even x]
-- >>> allEven2 [1, 2, 3, 4, 5, 6]
-- [2,4,6]
--
allEven3 :: [Integer] -> [Integer]
allEven3 = filter even
-- >>> allEven3 [1, 2, 3, 4, 5, 6]
-- [2,4,6]
--
-- Write a function that takes a list and returns the same list in reverse.
backwards :: [a] -> [a]
backwards = reverse
-- >>> backwards [1, 2, 3, 4]
-- [4,3,2,1]
--
backwards2 :: [a] -> [a]
backwards2 [] = []
backwards2 (x:xs) = backwards2 xs ++ [x]
-- >>> backwards2 [1, 2, 3, 4]
-- [4,3,2,1]
--
-- Write a function that builds two-tuples with all possible combinations of
-- two of the colors black, white, blue, yellow, and red. Note that you should
-- include only one of(black, blue)and(blue, black).
data Color = Black
| White
| Blue
| Yellow
| Red
deriving (Enum, Ord, Eq, Show)
-- https://stackoverflow.com/questions/4299319/getting-a-list-of-all-possible-data-type-values-in-haskell
allColors :: (Enum a) => [a]
allColors = [toEnum 0 ..]
pairs :: [(Color, Color)]
pairs = [(a, b) | a <- allColors, b <- allColors, a < b]
-- >>> pairs
-- [(Black,White),(Black,Blue),(Black,Yellow),(Black,Red),(White,Blue),(White,Yellow),(White,Red),(Blue,Yellow),(Blue,Red),(Yellow,Red)]
--