<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>fees <- mapM totalFeesOwed students</div><div>let total = sum fees</div><div><br></div><div>You can use a fold instead of sum if you want. </div><div><br></div><div>Will</div><div><br>On Jul 9, 2016, at 13:10, Guru Devanla <<a href="mailto:gurudev.devanla@gmail.com">gurudev.devanla@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr"><div>Say, in the above example, I want to add up values returned by `student_totalFeesOwed` by using foldM operation. Is it possible? <br><br></div><div>For example, here is an expression I have<br><br></div><div>L.foldr (\a b-> (evalState (student_totalFeesOwed a) $ env) + b) 0 [(RowId 1), (RowId 2)] <br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jul 9, 2016 at 9:15 AM, Will Yager <span dir="ltr"><<a href="mailto:will.yager@gmail.com" target="_blank">will.yager@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div></div><div>I did the same thing when I was learning to generalize my understanding of monads! Very common mistake. </div><div><br></div><div>I'm not sure I understand your question about #3. Can you give an example using evalState? We'll tell you if you can do it without evalState. </div><div><br></div><div>I suspect you want something like</div><div><br></div><div>"mapM_ addStudentFee students"</div><div><br></div><div>Will</div><div><div class="h5"><div><br>On Jul 9, 2016, at 00:56, Guru Devanla <<a href="mailto:gurudev.devanla@gmail.com" target="_blank">gurudev.devanla@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr"><div><div><div><div><div><div>William/Tom,<br><br></div>(1) Yes, looking into lens and re-factoring my current experimental project in lens will be my next iteration. For now, I plan not to spend time on it.<br><br></div>(2) Agreed. Not sure how I missed that.<br><br></div>(3) I see how foldM works now. I missed the point that foldM not only is a
`map` but also does a `sequence` after that. I got stuck earlier, thinking
I will end up with a list of state monads. The sequence steps executes
this monadic action.<br> <br></div>But, how can I do a foldM in a state monad. Say, I need to map over a list of students and add up all their fees, can I get away not `evalState` inside the foldM step function?<br><br></div>Thanks. this is very exciting as I keep simplifying my code!<br><br></div>Guru<br><div><div><div><br><br> </div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 8, 2016 at 7:55 PM, <span dir="ltr"><<a href="mailto:amindfv@gmail.com" target="_blank">amindfv@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><span><div></div><div><br></div><div><blockquote type="cite"><div class="gmail_extra"><div class="gmail_quote"><font color="#000000"><span style="background-color:rgba(255,255,255,0)">On Fri, Jul 8, 2016 at 9:57 PM, Guru Devanla <span dir="ltr"><<a href="mailto:gurudev.devanla@gmail.com" target="_blank">gurudev.devanla@gmail.com</a>></span> wrote:</span></font></div></div></blockquote></div><div><br></div></span><span><div><blockquote type="cite"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><font color="#000000"><span style="background-color:rgba(255,255,255,0)">1. I see that almost in every function I deal with state, I have e <- get , expression in the begining. I always ending up having to use the state to query for different values. I guess this is OK.</span></font></div></blockquote></div></div></blockquote></div></span><span><div><br>El 8 jul 2016, a las 22:07, William Yager <<a href="mailto:will.yager@gmail.com" target="_blank">will.yager@gmail.com</a>> escribió:<br><br></div><blockquote type="cite"><div><div dir="ltr"><div>For #1, look into using the Lens library's support for the State monad. You can often avoid doing a get, and instead write things like `fees += 5`, which will add 5 to the field in the state called "fees".</div><div><br></div></div></div></blockquote><div><br></div></span><div>Lens is a pretty heavy extra thing for a beginner to have to learn -- you'll do fine with the 'modify' function:</div><div><br></div><div>modify :: (s -> s) -> State s ()</div><div><br></div><div>So instead of writing:</div><div><br></div><div>do</div><div> s <- get</div><div> put (s + 5)</div><div><br></div><div>You say:</div><div><br></div><div>modify (+5)</div><div><br></div><div><br></div><div>Tom</div></div></blockquote></div><br></div>
</div></blockquote></div></div></div></blockquote></div><br></div>
</div></blockquote></body></html>