[L03 State] State monad

89 views
Skip to first unread message

David Tchepak

unread,
Feb 19, 2013, 10:07:43 PM2/19/13
to haskell-exercises
Next up I thought I'd have a go at L03 State.hs:

https://github.com/tonymorris/course/blob/master/src/L03/State.hs

There are tests for this: L03.State.Tests.main

Questions, comments and sample solutions welcomed.

Regards,
David

Riaan Rottier

unread,
Mar 6, 2013, 6:40:57 AM3/6/13
to haskell-...@googlegroups.com
I have a refactoring question again.  How do you refactor FilterM from my solution which is: 
filterM _ Nil = reeturn Nil
filterM p (x:|xs) = bind (\b -> (bind (\xs' -> reeturn (if b then x:|xs' else xs')) (filterM p xs))) (p x)

to a solution using fmaap?  

David Tchepak

unread,
Mar 7, 2013, 3:22:09 PM3/7/13
to haskell-exercises
Earlier we implemented fmaap as:

fmaap f = bind (\a -> reeturn (f a))

This is in the same shape as this snippet from `filterM`:

bind (\xs' -> reeturn (if b ...))

So we should be able to replace that bind/return with `fmaap` by using `(if b...)` as `f`.

I tend to think about this as unwrapping the monad value, applying the function, then re-wrapping using `return`. In those cases we can just map the function inside instead of using bind/return explicitly.

Hope this gets you un-stuck. Let me know if you'd like a break down of the steps to get it to `fmaap`.

Cheers,
David


--
You received this message because you are subscribed to the Google Groups "haskell-exercises" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haskell-exerci...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages