Last active
September 21, 2017 22:14
-
-
Save tonymorris/61ff53983694b32c0a76 to your computer and use it in GitHub Desktop.
This is how we add 10 to every element in a list
This file contains hidden or 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.Applicative | |
import Control.Monad.ST | |
import Control.Monad | |
import Data.STRef | |
while :: | |
Monad f => | |
f Bool | |
-> f a | |
-> f () | |
while p a = | |
p >>= (`when` (a >> while p a)) | |
map' :: | |
(a -> b) | |
-> [a] | |
-> [b] | |
map' f list = | |
runST $ do | |
l <- var id | |
c <- var list | |
while | |
(notEmpty <$> at c) | |
(do m <- at l | |
d <- at c | |
l .= (m . (f (head d):)) | |
c .= tail d) | |
($[]) <$> at l | |
add10 :: | |
[Int] | |
-> [Int] | |
add10 = | |
map' (+10) | |
-- support | |
notEmpty = not . null | |
at = readSTRef | |
var = newSTRef | |
(.=) = writeSTRef | |
infixr 4 .= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wicked.