[Haskell-cafe] List Monads and non-determinism

Chris Wong chrisyco+haskell-cafe at gmail.com
Sat Jul 20 01:25:53 CEST 2013


> I thought >>= was left associative?  It seems to be in the examples from
> Learn You A Haskell.

It is. But lambdas are parsed using the "maximal munch" rule, so they
extend *as far to the right as possible*.

So

    \x -> x * 2 + 1

would be parsed as

    \x -> (x * 2 + 1)  -- right

not

    (\x -> x) * 2 + 1  -- wrong

which is obviously incorrect.

I believe C uses a similar rule for funny expressions like `x+++y`
(using maximal munch: `(x++) + y`).

>
> I tried to use the associative law to bracket from the right but it didn't
> like that either...
>
> [1,2] >>= (\x -> (\n -> [3,4])) x  >>= \m -> return (n,m))
>
> Any thoughts?
>
> Matt
>
> On 19 Jul 2013, at 23:35, Rogan Creswick <creswick at gmail.com> wrote:
>
> On Fri, Jul 19, 2013 at 3:23 PM, Matt Ford <matt at dancingfrog.co.uk> wrote:
>>
>> I started by putting brackets in
>>
>> ([1,2] >>= \n -> [3,4]) >>= \m -> return (n,m)
>>
>> This immediately fails when evaluated: I expect it's something to do
>> with the n value now not being seen by the final return.
>
>
> You're bracketing from the wrong end, which your intuition about n's
> visibility hints at.  Try this as your first set of parens:
>
>  [1,2] >>= (\n -> [3,4] >>= \m -> return (n,m))
>
> --Rogan
>
>>
>>
>> It seems to me that the return function is doing something more than
>> it's definition (return x = [x]).
>>
>> If ignore the error introduced by the brackets I have and continue to
>> simplify I get.
>>
>> [3,4,3,4] >>= \m -> return (n,m)
>>
>> Now this obviously won't work as there is no 'n' value.  So what's
>> happening here? Return seems to be doing way more work than lifting the
>> result to a list, how does Haskell know to do this?  Why's it not in the
>> function definition?  Are lists somehow a special case?
>>
>> Any pointers appreciated.
>>
>> Cheers,
>>
>> --
>> Matt
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



--
Chris Wong, fixpoint conjurer
  e: lambda.fairy at gmail.com
  w: http://lfairy.github.io/




More information about the Haskell-Cafe mailing list