[Haskell-cafe] Haskell Data Structure design
Guru Devanla
gurudev.devanla at gmail.com
Sun Jul 10 01:35:08 UTC 2016
That is a nice trick! Thanks
On Jul 9, 2016 1:22 PM, "Will Yager" <will.yager at gmail.com> wrote:
> I believe you could use
>
> foldM (\sum student -> (sum +) <$> totalFeesOwed student) 0 students
>
> Will
>
> On Jul 9, 2016, at 15:09, Guru Devanla <gurudev.devanla at gmail.com> wrote:
>
> OK. Thank you. That is what I ended up doing after I understood how mapM
> worked. But, since I was going through 2 steps, I was wondering if using
> foldM directly was possible in this case.
>
>
> On Sat, Jul 9, 2016 at 11:25 AM, Will Yager <will.yager at gmail.com> wrote:
>
>> fees <- mapM totalFeesOwed students
>> let total = sum fees
>>
>> You can use a fold instead of sum if you want.
>>
>> Will
>>
>> On Jul 9, 2016, at 13:10, Guru Devanla <gurudev.devanla at gmail.com> wrote:
>>
>> Say, in the above example, I want to add up values returned by
>> `student_totalFeesOwed` by using foldM operation. Is it possible?
>>
>> For example, here is an expression I have
>>
>> L.foldr (\a b-> (evalState (student_totalFeesOwed a) $ env) + b) 0
>> [(RowId 1), (RowId 2)]
>>
>> On Sat, Jul 9, 2016 at 9:15 AM, Will Yager <will.yager at gmail.com> wrote:
>>
>>> I did the same thing when I was learning to generalize my understanding
>>> of monads! Very common mistake.
>>>
>>> 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.
>>>
>>> I suspect you want something like
>>>
>>> "mapM_ addStudentFee students"
>>>
>>> Will
>>>
>>> On Jul 9, 2016, at 00:56, Guru Devanla <gurudev.devanla at gmail.com>
>>> wrote:
>>>
>>> William/Tom,
>>>
>>> (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.
>>>
>>> (2) Agreed. Not sure how I missed that.
>>>
>>> (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.
>>>
>>> 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?
>>>
>>> Thanks. this is very exciting as I keep simplifying my code!
>>>
>>> Guru
>>>
>>>
>>>
>>>
>>> On Fri, Jul 8, 2016 at 7:55 PM, <amindfv at gmail.com> wrote:
>>>
>>>>
>>>> On Fri, Jul 8, 2016 at 9:57 PM, Guru Devanla <gurudev.devanla at gmail.com
>>>> > wrote:
>>>>
>>>>
>>>> 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.
>>>>>
>>>>
>>>> El 8 jul 2016, a las 22:07, William Yager <will.yager at gmail.com>
>>>> escribiÃ³:
>>>>
>>>> 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".
>>>>
>>>>
>>>> Lens is a pretty heavy extra thing for a beginner to have to learn --
>>>> you'll do fine with the 'modify' function:
>>>>
>>>> modify :: (s -> s) -> State s ()
>>>>
>>>> So instead of writing:
>>>>
>>>> do
>>>> s <- get
>>>> put (s + 5)
>>>>
>>>> You say:
>>>>
>>>> modify (+5)
>>>>
>>>>
>>>> Tom
>>>>
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160709/4e86e4ae/attachment.html>
More information about the Haskell-Cafe
mailing list