-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* stack-safe foldl * name TCOd foldl fodlRec * add stack-safe foldl' * Adapt runListT from Haskell's List.Transformer * ListT examples * simplify runListT Unnecessary use of `void action`. * add runListTRec and use in examples
- Loading branch information
1 parent
e680771
commit cc045ea
Showing
3 changed files
with
49 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Example.List where | ||
|
||
import Prelude | ||
import Data.Array as A | ||
import Control.Monad.Eff (Eff) | ||
import Control.Monad.Eff.Class (liftEff) | ||
import Control.Monad.Eff.Console (CONSOLE, log) | ||
import Control.Monad.List.Trans (ListT, runListTRec, iterate, takeWhile) | ||
import Control.MonadZero (guard) | ||
|
||
-- based on http://hackage.haskell.org/package/list-transformer | ||
logList :: forall eff. | ||
ListT (Eff (console :: CONSOLE | eff)) String | ||
-> Eff (console :: CONSOLE | eff) Unit | ||
logList l = runListTRec do | ||
liftEff $ log "logging listT" | ||
str <- l | ||
liftEff $ log str | ||
|
||
-- based on https://wiki.haskell.org/ListT_done_right#Sum_of_squares | ||
sumSqrs :: forall eff. | ||
Int | ||
-> ListT (Eff (console :: CONSOLE | eff)) Unit | ||
sumSqrs n = do | ||
let | ||
nats = iterate (add one) zero | ||
squares = takeWhile (_ <= n) $ map (\x -> x * x) nats | ||
x <- squares | ||
y <- squares | ||
liftEff $ log ("<" <> show x <> "," <> show y <> ">") | ||
guard $ x + y == n | ||
liftEff $ log "Sum of squares." | ||
|
||
main :: forall eff. Eff (console :: CONSOLE | eff) Unit | ||
main = do | ||
logList $ A.toUnfoldable ["one", "two", "three"] | ||
runListTRec $ sumSqrs 10 |
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