From be6ac5d3a1cc92d2e187787952aa7f340ba7590d Mon Sep 17 00:00:00 2001 From: Jason Staten Date: Sun, 19 Jul 2020 21:43:51 -0600 Subject: [PATCH] day one file --- .gitignore | 1 + haskell/DayOne.hs | 66 +++++++++++++++++++++++++++++++++++++++++++++ haskell/Main.hs | 2 ++ haskell/all_even.hs | 21 --------------- 4 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 haskell/DayOne.hs delete mode 100644 haskell/all_even.hs diff --git a/.gitignore b/.gitignore index 3d6705f..e3f9c03 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ project/metals.sbt nashorn_code_cache out .nrepl-port +dist-newstyle diff --git a/haskell/DayOne.hs b/haskell/DayOne.hs new file mode 100644 index 0000000..7ce78bf --- /dev/null +++ b/haskell/DayOne.hs @@ -0,0 +1,66 @@ +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)] +-- + diff --git a/haskell/Main.hs b/haskell/Main.hs index 65ae4a0..2519053 100644 --- a/haskell/Main.hs +++ b/haskell/Main.hs @@ -1,4 +1,6 @@ module Main where +import DayOne + main :: IO () main = putStrLn "Hello, Haskell!" diff --git a/haskell/all_even.hs b/haskell/all_even.hs deleted file mode 100644 index d3b98c4..0000000 --- a/haskell/all_even.hs +++ /dev/null @@ -1,21 +0,0 @@ -module Main where - allEven :: [Integer] -> [Integer] - allEven [] = [] - allEven (h:t) = if even h then h:allEven t else allEven t - - allEven2 :: [Integer] -> [Integer] - allEven2 xs = [x | x <- xs, even x] - - allEven3 :: [Integer] -> [Integer] - allEven3 = filter even - - -- >>> allEven [1, 2, 3, 4, 5, 6] - -- [2,4,6] - -- >>> allEven2 [1, 2, 3, 4, 5, 6] - -- [2,4,6] - -- >>> allEven3 [1, 2, 3, 4, 5, 6] - -- [2,4,6] - - main :: IO() - main = - print "yes" \ No newline at end of file