Last active
January 26, 2020 23:23
-
-
Save gustavofranke/ccee17cbc42e21d6923be61ea07433ea to your computer and use it in GitHub Desktop.
my solutions to pchiusano's amazing gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad | |
import Control.Applicative | |
-- | from https://gist.github.com/pchiusano/bf06bd751395e1a6d09794b38f093787 | |
-- 1. Sum up a `[Int]`, using explicit recursion. (nuts and bolts) | |
sum' :: [Int] -> Int | |
sum' [] = 0 | |
sum' (i:is) = i + (sum' is) | |
-- 2. Write a function to reverse a list, including its signature. | |
-- If possible, use one of the fold combinators, rather than explicit recursion. (nuts and bolts) | |
rev :: [a] -> [a] | |
rev = foldl (\b a -> (a:b)) [] | |
-- 3. Implement `filter` for lists. (nuts and bolts) | |
filter' :: (a -> Bool) -> [a] -> [a] | |
filter' cond [] = [] | |
filter' cond (x:xs) = if cond x then x:rest else rest | |
where rest = (filter' cond xs) | |
-- 4. Implement `filter` and `takeWhile` using `foldr`. (nuts and bolts) | |
filter'' :: (a -> Bool) -> [a] -> [a] | |
filter'' cond (x:xs) = foldr (\x xs -> if cond x then x:xs else xs) [] (x:xs) | |
takeWhile' :: (a -> Bool) -> [a] -> [a] | |
takeWhile' cond (x:xs) = foldr (\x xs -> if cond x then x:xs else []) [] (x:xs) | |
-- 5. Implement functions with the following types: (reasoning about types) | |
-- * `forall a . a -> a` | |
-- * `forall a . a -> (a, a)` | |
-- * `forall a b . (a -> b) -> a -> b` | |
-- * `forall a b c . (a -> b -> c) -> (a,b) -> c` | |
one :: a -> a | |
one = id | |
two :: a -> (a, a) | |
two = \x -> (t x, t x) | |
where t = one | |
three :: (a -> b) -> a -> b | |
three f a0 = f $ a0 | |
-- *Main> three (\x -> show x) 8 | |
-- "8" | |
-- *Main> three (\x -> show x) "p" | |
-- "\"p\"" | |
-- *Main> three (\x -> show x) 'p' | |
-- "'p'" | |
four :: (a -> b -> c) -> (a,b) -> c | |
four f t = (f $ (fst t) ) (snd t) | |
-- *Main> four (\x y -> show x ++ show y) ("p",True) | |
-- "\"p\"True" | |
-- 7. Give the `Monad` instance for `Maybe`. Explain the meaning of `liftM2` for `Maybe`. (knowledge of functional structures) | |
data Maybe' a = Nothing' | Just' a deriving (Eq, Ord, Show) | |
instance Functor Maybe' where | |
fmap f Nothing' = Nothing' | |
fmap f (Just' a) = Just' (f a) | |
instance Applicative Maybe' where | |
pure = Just' | |
Nothing' <*> _ = Nothing' | |
(Just' f) <*> something = fmap f something | |
instance Monad Maybe' where | |
(Just' a) >>= amb = amb a | |
----------- | |
-- *Main> Just' 5 | |
-- Just' 5 | |
-- *Main> fmap (+ 1) (Just' 5) | |
-- Just' 6 | |
-- *Main> (Just' 4) <*> (Just' 1) | |
-- *Main> (Just' (\x -> show (x + 1))) <*> (Just' 5) | |
-- Just' "6" | |
-- *Main> (Just' 5) >>= (\x -> Just' (x + 1)) | |
-- Just' 6 | |
----------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment