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:12 (UTC -07:00)
View GitHub Profile
@ConnorBaker
ConnorBaker / 5-3.hs
Last active February 23, 2019 00:51
Programming in Haskell, 2nd ed.: Chapter 5: Problem 3
grid :: Int -> Int -> [(Int,Int)]
grid m n = [(x,y) | x <- [0..m], y <- [0..n]]
square :: Int -> [(Int,Int)]
square n = [(x,y) | (x,y) <- grid n n, x /= y]
@ConnorBaker
ConnorBaker / 5-4.hs
Last active February 23, 2019 00:51
Programming in Haskell, 2nd ed.: Chapter 5: Problem 4
replicate :: Int -> a -> [a]
replicate n x = [x | _ <- [1..n]]
@ConnorBaker
ConnorBaker / 5-5.hs
Last active February 23, 2019 00:51
Programming in Haskell, 2nd ed.: Chapter 5: Problem 5
pyths :: Int -> [(Int,Int,Int)]
pyths n = [(a,b,c) | c <- [1..n],
b <- [1..c],
a <- [1..b],
a^2 + b^2 == c^2]
@ConnorBaker
ConnorBaker / 5-6.hs
Last active February 23, 2019 00:51
Programming in Haskell, 2nd ed.: Chapter 5: Problem 6
factors :: Int -> [Int]
factors n = [x | x <- [1..n], n `mod` x == 0]
perfects :: Int -> [Int]
perfects n = [x | x <- [1..n],
x == sum (factors x) - ]
@ConnorBaker
ConnorBaker / 5-7.hs
Last active February 23, 2019 00:51
Programming in Haskell, 2nd ed.: Chapter 5: Problem 7
list1 :: [(Int,Int)]
list1 = concat [[(x,y) | y <- [3,4]] | x <- [1,2]]
@ConnorBaker
ConnorBaker / 5-8.hs
Last active February 23, 2019 00:50
Programming in Haskell, 2nd ed.: Chapter 5: Problem 8
find :: Eq a => a -> [(a,b)] -> [b]
find k t = [v | (k',v) <- t, k == k']
positions :: Eq a => a -> [a] -> [Int]
positions x xs = [i | (x',i) <- zip xs [0..], x == x']
positions' :: Eq a => a -> [a] -> [Int]
positions' x xs = find x (zip xs [0..])
@ConnorBaker
ConnorBaker / 5-9.hs
Last active February 23, 2019 00:50
Programming in Haskell, 2nd ed.: Chapter 5: Problem 9
chisqr :: [Float] -> [Float] -> Float
chisqr os es = sum [((o-e)^2)/e | (o,e) <- zip os es]
scalarproduct :: [Int] -> [Int] -> Int
scalarproduct xs ys = sum [x*y | (x,y) <- zip xs ys]
@ConnorBaker
ConnorBaker / 5-10.hs
Last active February 23, 2019 00:50
Programming in Haskell, 2nd ed.: Chapter 5: Problem 10
-- We assume that the input is not only upper case letters.
import Data.Char
-- Returns a list of indices of the specifed value
positions :: Eq a => a -> [a] -> [Int]
positions x xs = [i | (x',i) <- zip xs [0..], x == x']
-- Returns the number of lowercase characters
lowers :: String -> Int
lowers xs = length [x | x <- xs, 'a' <= x && x <= 'z']
@ConnorBaker
ConnorBaker / 6-1.hs
Last active February 23, 2019 00:50
Programming in Haskell, 2nd ed.: Chapter 6: Problem 1
facRecursive :: Int -> Int
facRecursive n
| n < 0 = 0
| n == 0 = 1
| otherwise = n * facRecursive (n - 1)
@ConnorBaker
ConnorBaker / 6-2.hs
Last active February 23, 2019 00:50
Programming in Haskell, 2nd ed.: Chapter 6: Problem 2
sumdown :: Int -> Int
sumdown 0 = 0
sumdown n = n + sumdown (n - 1)