Skip to content

Instantly share code, notes, and snippets.

@googya
Created December 20, 2017 10:16
Show Gist options
  • Save googya/895c8ccfd086404701b49d6394b421cb to your computer and use it in GitHub Desktop.
Save googya/895c8ccfd086404701b49d6394b421cb to your computer and use it in GitHub Desktop.
module Apl1 where
import Control.Applicative
import Data.Monoid
data List a =
Nil
| Cons a (List a)
deriving (Eq, Show)
append :: List a -> List a -> List a
append Nil ys = ys
append (Cons x xs) ys = Cons x $ (xs `append` ys)
fold :: (a -> b -> b) -> b -> List a -> b
fold _ b Nil = b
fold f b (Cons h t) = f h (fold f b t)
concat' :: List (List a) -> List a
concat' = fold append Nil
flatMap :: (a -> List b) -> List a -> List b
flatMap f ts = concat' $ fmap f ts
-- functions :: List Integer
-- functions = Cons (+1) (Cons (*2) Nil)
-- values :: List Integer
-- values = Cons 1 (Cons 2 Nil)
instance Functor List where
fmap _ Nil = Nil
fmap f (Cons a b) = Cons (f a) (fmap f b)
instance Applicative List where
pure _ = Nil
Nil <*> _ = Nil
_ <*> Nil = Nil
(Cons f k) <*> xs = append (fmap f xs) (k <*> xs)
take' :: Int -> List a -> List a
take' = undefined
newtype ZipList' a = ZipList' (List a) deriving (Eq, Show)
instance Functor ZipList' where
fmap f (ZipList' xs) = ZipList' $ fmap f xs
instance Applicative ZipList' where
pure = ZipList' Nil
(ZipList' Nil) <*> _ = ZipList' Nil
_ <*> (ZipList' Nil) = ZipList' Nil
(ZipList' (Const f fs)) <*> (ZipList' (Cons x xs)) = (f n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment