Last active
August 29, 2015 14:24
-
-
Save theadam/a2cd0b713de06d3b61a9 to your computer and use it in GitHub Desktop.
purescript lazy 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
| module Main where | |
| import Control.Monad.Eff | |
| import Debug.Trace | |
| foreign import data Random :: ! | |
| foreign import data Lazy :: * -> * | |
| foreign import defer """ | |
| function Defer (thunk) { | |
| this.thunk = thunk; | |
| return this; | |
| } | |
| function defer(thunk){ | |
| return new Defer(thunk); | |
| } | |
| Defer.prototype.force = function () { | |
| var value = this.thunk(); | |
| delete this.thunk; | |
| this.force = function () { | |
| return value; | |
| }; | |
| return value; | |
| }; | |
| """ :: forall a. (Unit -> a) -> Lazy a | |
| -- | Force evaluation of a `Lazy` value. | |
| foreign import force """ | |
| function force(l){ | |
| return l.force(); | |
| } | |
| """ :: forall a. Lazy a -> a | |
| data LList a = Empty | Cons a (Lazy (LList a)) | |
| infixr 6 ::: | |
| (:::) :: forall a. a -> (Lazy (LList a)) -> LList a | |
| (:::) = Cons | |
| instance showLlist :: (Show a) => Show (LList a) where | |
| show Empty = "" | |
| show (Cons x y) = (show x) ++ comma ++ tail | |
| where | |
| tail = show $ force y | |
| comma = case tail of | |
| Empty -> "" | |
| Cons _ _ -> "," | |
| main = print $ (1 ::: defer (\_ -> Empty)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment