Skip to content

Instantly share code, notes, and snippets.

@seansu4you87
Created August 1, 2014 00:31
Show Gist options
  • Save seansu4you87/933808efd75b29c59ae0 to your computer and use it in GitHub Desktop.
Save seansu4you87/933808efd75b29c59ae0 to your computer and use it in GitHub Desktop.
Haskell for Taquerias
foo :: Int
foo = 5
incr :: Int -> Int
incr x = x + 1
decr :: Int -> Int
decr x = x - 1
nada :: Int -> Int
nada = incr . decr
nada' :: Int -> Int
nada' x = incr $ decr 1
add :: Int -> Int -> Int
add x y = x + y
fac :: Int -> Int
fac 1 = 1
fac n | n > 1 = n * fac (n-1)
fac _ = error "bad"
fib :: Int -> Int
fib 1 = 1
fib 2 = 1
fib n = fib (n-1) + fib (n-2)
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
map' :: (a -> b) -> [a] -> [b]
map' f [] = []
map' f (x:xs) = (f x):(map' f xs)
filter' :: (a -> Bool) -> [a] -> [a]
filter' f [] = []
filter' f (x:xs) | f x = x : filter' f xs
| otherwise = filter' f xs
-- Data
data Salsa = Tomatillo | Chipotle | Mango | Custom String | Mix [Salsa]
explode :: [String] -> String -> String
explode [] _ = []
explode [x] _ = x
explode (x:xs) separator = x ++ separator ++ explode xs separator
salsaDescription :: Salsa -> String
salsaDescription Tomatillo = "green"
salsaDescription Chipotle = "smokey"
salsaDescription Mango = "sweet"
salsaDescription (Custom desc) = desc ++ " salsa"
salsaDescription (Mix salsas) = "Mix of " ++ explode (map salsaDescription salsas) ", "
data List a = ListEmpty | Node a (List a)
list1 = ListEmpty
list2 = (Node 5 ListEmpty)
list3 = (Node 5 ( Node 6 ListEmpty))
data Tree a = TreeEmpty | TreeNode a (Tree a) (Tree a)
tree1 = TreeEmpty
tree2 = TreeNode 6
TreeEmpty
(TreeNode 6 TreeEmpty TreeEmpty)
sumTree :: (Tree Int) -> Int
sumTree TreeEmpty = 0
sumTree (TreeNode v left right) = v + (sumTree left) + (sumTree right)
salsaPreferences = [("Raphie", Chipotle), ("Lyall", Mango)]
salsaGreeting :: String -> String
salsaGreeting name = "Hey " ++ name ++ ", your favorite salsa is " ++ safeDescription salsa
where salsa = lookup name salsaPreferences
safeDescription Nothing = "an unknown salsa"
safeDescription (Just salsa) = salsaDescription salsa
wallize :: Int -> String
wallize n = (show n) ++ " beers on the wall"
beersOnTheWall :: Int -> [String]
-- beersOnTheWall 0 = []
-- beersOnTheWall n = (beersOnTheWall (n-1)) ++ [wallize n]
-- Lazy Approach:
beersOnTheWall n = take n (map wallize [1..])
-- main = interact (unlines . reverse . lines)
main = interact reverse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment