Skip to content

Instantly share code, notes, and snippets.

View ConnorBaker's full-sized avatar
⚱️
burnt out

Connor Baker ConnorBaker

⚱️
burnt out
  • Costa Mesa, CA
  • 21:31 (UTC -08:00)
View GitHub Profile
@ConnorBaker
ConnorBaker / 4-1.hs
Last active February 23, 2019 00:52
Programming in Haskell, 2nd ed.: Chapter 4: Problem 1
{-
As interesting side reading, consult https://www.reddit.com/r/haskell/comments/1m9opl/passing_list_as_arguments/
-}
halve :: [a] -> ([a],[a])
halve xs = splitAt (((length xs) + 1) `div` 2) xs
@ConnorBaker
ConnorBaker / 4-2.hs
Last active February 23, 2019 00:52
Programming in Haskell, 2nd ed.: Chapter 4: Problem 2
thirdHeadAndTail :: [a] -> a
thirdHeadAndTail = head . tail . tail
thirdIndex :: [a] -> a
thirdIndex xs = xs !! 2
thirdPatternMatching :: [a] -> a
thirdPatternMatching (_:_:x:_) = x
@ConnorBaker
ConnorBaker / 4-3.hs
Last active February 23, 2019 00:52
Programming in Haskell, 2nd ed.: Chapter 4: Problem 3
safetailConditional :: [a] -> [a]
safetailConditional xs = if null xs
then []
else tail xs
safetailGuarded :: [a] -> [a]
safetailGuarded xs | null xs = []
| otherwise = tail xs
safetailPatternMatch :: [a] -> [a]
@ConnorBaker
ConnorBaker / 4-4.hs
Last active February 23, 2019 00:52
Programming in Haskell, 2nd ed.: Chapter 4: Problem 4
or1 :: Bool -> Bool -> Bool
True `or1` True = True
True `or1` False = True
False `or1` True = True
False `or1` False = True
or2 :: Bool -> Bool -> Bool
False `or2` False = False
_ `or2` _ = True
@ConnorBaker
ConnorBaker / 4-5.hs
Last active February 23, 2019 00:52
Programming in Haskell, 2nd ed.: Chapter 4: Problem 5
formalConjunction :: Bool -> Bool -> Bool
formalConjunction a b =
if a then
if b then
True
else False
else False
@ConnorBaker
ConnorBaker / 4-6.hs
Last active February 23, 2019 00:52
Programming in Haskell, 2nd ed.: Chapter 4: Problem 6
formalConjunction :: Bool -> Bool -> Bool
formalConjunction a b =
if a then
b
else
False
@ConnorBaker
ConnorBaker / 4-7.hs
Last active February 23, 2019 00:51
Programming in Haskell, 2nd ed.: Chapter 4: Problem 7
mult :: Int -> Int -> Int -> Int
mult = \x -> (\y -> (\z -> x * y * z))
@ConnorBaker
ConnorBaker / 4-8.hs
Last active February 23, 2019 00:51
Programming in Haskell, 2nd ed.: Chapter 4: Problem 8
luhnDouble :: Int -> Int
luhnDouble n | n <= 5 = 2 * n
| otherwise = 2 * n - 9
luhn :: Int -> Int -> Int -> Int -> Bool
luhn a b c d =
(luhnDouble a + b + luhnDouble c + d)
`mod` 10 == 0
@ConnorBaker
ConnorBaker / 5-1.hs
Last active February 23, 2019 00:51
Programming in Haskell, 2nd ed.: Chapter 5: Problem 1
sum100ConsecSquares :: Int
sum100ConsecSquares = sum [x^2 | x <- [1..100]]
@ConnorBaker
ConnorBaker / 5-2.hs
Last active February 23, 2019 00:51
Programming in Haskell, 2nd ed.: Chapter 5: Problem 2
grid :: Int -> Int -> [(Int,Int)]
grid m n = [(x,y) | x <- [0..m], y <- [0..n]]