Created
December 16, 2015 15:03
-
-
Save Jiggins/6922ddcc0186591e003d to your computer and use it in GitHub Desktop.
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
module Maybe where | |
import Prelude hiding (Maybe (..)) | |
-- Take a look at Hackage: Data.Maybe | |
-- https://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Maybe.html | |
data Maybe a = Nothing | |
| Just a | |
deriving (Eq, Show, Ord, Read) | |
instance Functor Maybe where | |
fmap f Nothing = Nothing | |
fmap f (Just x) = Just (f x) | |
instance Applicative Maybe where | |
pure = Just | |
Nothing <*> _ = Nothing | |
Just f <*> Nothing = Nothing | |
Just f <*> (Just x) = Just (f x) | |
instance Monad Maybe where | |
return x = Just x | |
Nothing >>= _ = Nothing | |
(Just x) >>= f = f x | |
module Test where | |
import Prelude hiding (sum, map) | |
sum' :: Num a => [a] -> a | |
sum' [] = 0 | |
sum' xs = head xs + sum' (tail xs) | |
sum'' :: Num a => [a] -> a | |
sum'' [] = 0 | |
sum'' (x:xs) = x + sum'' xs | |
sum''' :: Num a => [a] -> a | |
sum''' xs = foldl (+) 0 xs | |
sum :: (Foldable t, Num a) => t a -> a | |
sum = foldl (+) 0 | |
map f [] = [] | |
map f (x:xs) = f x : map f xs | |
-- data [] a = [] | |
-- | a : [a] | |
data Tree a = Empty | |
| Tree a (Tree a) (Tree a) | |
deriving (Eq, Show, Read) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment