~statianzo/sevenlangs

be6ac5d3a1cc92d2e187787952aa7f340ba7590d — Jason Staten 2 years ago 18767fb
day one file
4 files changed, 69 insertions(+), 21 deletions(-)

M .gitignore
A haskell/DayOne.hs
M haskell/Main.hs
D haskell/all_even.hs
M .gitignore => .gitignore +1 -0
@@ 8,3 8,4 @@ project/metals.sbt
nashorn_code_cache
out
.nrepl-port
dist-newstyle

A haskell/DayOne.hs => haskell/DayOne.hs +66 -0
@@ 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)]
--


M haskell/Main.hs => haskell/Main.hs +2 -0
@@ 1,4 1,6 @@
module Main where

import DayOne

main :: IO ()
main = putStrLn "Hello, Haskell!"

D haskell/all_even.hs => haskell/all_even.hs +0 -21
@@ 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