This is how we add 10 to every element in a list
| 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
This comment has been minimized.
Wicked.