Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  What is a "Monad Comprehension" ? (Michael Orlitzky)
   2. Re:  State as function? (martin)
   3. Re:  State as function? (Kim-Ee Yeoh)
   4. Re:  State as function? (Karl Voelker)
   5. Re:  State as function? (Kim-Ee Yeoh)
   6.  set multiplication (Nishant)
   7. Re:  set multiplication (Mariano P?rez Rodr?guez)
   8. Re:  Problem combining monads <sigh!> (Kim-Ee Yeoh)


----------------------------------------------------------------------

Message: 1
Date: Mon, 07 Apr 2014 09:05:42 -0400
From: Michael Orlitzky <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] What is a "Monad Comprehension" ?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On 04/07/2014 04:33 AM, John M. Dlugosz wrote:
> I know about List Comprehensions, but what is a general Monad Comprehension?  
> List 
> Comprehensions are the use of set-builder notation to define a list, so I 
> don't understand 
> how that would generalize.

The set-builder notation to find the sums of elements from two lists
looks like,

  list_sums = [ x + y | x <- [1,2,3], y <- [4,5,6] ]

The way the list monad is defined, this is the same thing as,

  list_sums = do
    x <- [1, 2, 3]
    y <- [4, 5, 6]
    return (x + y)

The set-builder notation is not obviously generalizable, but once you
desugar it to do-notation, it's more clear that you could do the same
thing with any monad. What actually happens might not be intuitive, but
the desugaring will work at least. For the Maybe monad, you could do
something like,

  maybe_sums = [ x + y | x <- Just 1, y <- Just 2 ]

which will be desugared into,

  maybe_sums = do
    x <- Just 1
    y <- Just 2
    return (x + y)

Any other monad will work the same.



------------------------------

Message: 2
Date: Mon, 07 Apr 2014 20:43:34 +0200
From: martin <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] State as function?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Am 04/06/2014 07:39 PM, schrieb Karl Voelker:
> On Sun, Apr 6, 2014, at 09:28 AM, martin wrote:
>> The problem with this approach is that I end up with two representations
>> of GuitarString. One is the accessor functions
>> above, but there is also a datatype GuitarString which I need to express
>> things like "put your finger on the nth fret of
>> the mth string".
> 
> You could try using an array:
> 
> data StringIndex = S1 | S2 | S3 | S4 | S5 | S6 deriving (Eq, Ord, Enum,
> Bounded, Read, Show)
> 
> type Guitar = Array StringIndex StringState
> 
> http://hackage.haskell.org/package/array-0.5.0.0/docs/Data-Array.html
> 
> The Enum and Bounded constraints on StringIndex let you do fun things
> like:
> 
> allStrings = [minBound .. maxBound]

Thanks Karl, this tidies up my code quite a bit.
BTW: I named the StringIndexes E6 | A | D | G | B | E1 which reflects the way a 
guitar is tuned. Way cool.




------------------------------

Message: 3
Date: Tue, 8 Apr 2014 02:00:05 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] State as function?
Message-ID:
        <CAPY+ZdQ2RFH3OB5EmQ7hJXGmyMssTTzHTM-3FNgn8E85xtQ=q...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On Tue, Apr 8, 2014 at 1:43 AM, martin <[email protected]> wrote:

> BTW: I named the StringIndexes E6 | A | D | G | B | E1 which reflects the
> way a guitar is tuned. Way cool.


Nice :)

This n-tuple as Array trick is something I missed. Thanks, Karl. Do you
know where it's been used?

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140408/1c976423/attachment-0001.html>

------------------------------

Message: 4
Date: Mon, 07 Apr 2014 19:58:42 -0700
From: Karl Voelker <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] State as function?
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Mon, Apr 7, 2014, at 12:00 PM, Kim-Ee Yeoh wrote:


On Tue, Apr 8, 2014 at 1:43 AM, martin <[1][email protected]>
wrote:

  BTW: I named the StringIndexes E6 | A | D | G | B | E1 which
  reflects the way a guitar is tuned. Way cool.


Nice :)

This n-tuple as Array trick is something I missed. Thanks, Karl. Do you
know where it's been used?


The last time I used this technique was when I needed to represent the
state of a mancala board. I may have seen it done somewhere else, but I
don't remember.

-Karl

References

1. mailto:[email protected]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140407/6cebdbb7/attachment-0001.html>

------------------------------

Message: 5
Date: Tue, 8 Apr 2014 10:33:39 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] State as function?
Message-ID:
        <capy+zdrab-sz3gzihcs6cptswsp3xc6g+qafqqynmqh8exe...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On Tue, Apr 8, 2014 at 9:58 AM, Karl Voelker <[email protected]> wrote:

> The last time I used this technique was when I needed to represent the
> state of a mancala board. I may have seen it done somewhere else, but I
> don't remember.
>

Ah thanks. I'll be sure to stay on the lookout for this.

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140408/446cd716/attachment-0001.html>

------------------------------

Message: 6
Date: Tue, 8 Apr 2014 10:30:01 +0530
From: Nishant <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] set multiplication
Message-ID:
        <CAEQDmz6VCLg2x86phtGmHV16=oveqfkk8-2tr88tgyzv+ko...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

hi,

I am trying to implement a set multiplication program in haskell.

Input : [1,2,3]   [4,5]  [6,7]
Ouput : [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...]


I implemented it for constant number of inputs like

setMul xs ys zs =  [ [x] ++ [y] ++ [z] |  x <- xs , y<-ys ,z <- zs]

I am not able to generalize this for any number of lists.

type signature would be :

setMulMany :: [[a]] -> [[a]]

Example :

Input : [ [1,2,3] , [4,5] , [6,7]]
Ouput :  [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...]



Regards.
Nishant
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140408/643ac8f9/attachment-0001.html>

------------------------------

Message: 7
Date: Tue, 8 Apr 2014 02:28:27 -0300
From: Mariano P?rez Rodr?guez  <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] set multiplication
Message-ID:
        <caaiex4cahdyc2uracfm8nmv7ytwwr_ddlejxuzxen54njo3...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

It's been a while since I last wrote any Haskell code, but try:

setMulMany :: [[a]] -> [[a]]
setMulMany = foldr mulOne [[]]
    where
        mulOne :: [a] -> [[a]] -> [[a]]
        mulOne xs yss = [ x:ys | x <- xs, ys <- yss]

I'm writing this from my phone, so didn't have a chance to test it really...

Hope it helps!
On Apr 8, 2014 2:00 AM, "Nishant" <[email protected]> wrote:

>
> hi,
>
> I am trying to implement a set multiplication program in haskell.
>
> Input : [1,2,3]   [4,5]  [6,7]
> Ouput : [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...]
>
>
> I implemented it for constant number of inputs like
>
> setMul xs ys zs =  [ [x] ++ [y] ++ [z] |  x <- xs , y<-ys ,z <- zs]
>
> I am not able to generalize this for any number of lists.
>
> type signature would be :
>
> setMulMany :: [[a]] -> [[a]]
>
> Example :
>
> Input : [ [1,2,3] , [4,5] , [6,7]]
> Ouput :  [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...]
>
>
>
> Regards.
> Nishant
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140408/8693b12a/attachment-0001.html>

------------------------------

Message: 8
Date: Tue, 8 Apr 2014 12:54:17 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Problem combining monads <sigh!>
Message-ID:
        <CAPY+ZdS8iMSn+pVtoMP1q_p=tcjdsgmp+4zfrocq-vwy0oc...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

John,

This is one MAJOR hurdle for newcomers to get over and pretty much everyone
stumbles and falls into typed combinator enlightenment. Eventually.

Let's ask ghci what some of the types are:

    :t [1,2,3]
    [1,2,3] :: [Int]

    :t (+7)
    (+7) :: Int -> Int

    :t (<$>)
    (<$>) :: Functor f => (a -> b) -> f a -> f b

So in ((+7) <$>) we have

    (<$>) :: Functor f => (a -> b) -> f a -> f b

applied to (note the _type_ of the first argument: a -> b):

    (+7) :: Int -> Int

Now the type variables a and b must match up, so we have a=b=Int.

And now we have

    ((+7) <$>) :: Functor f => f Int - f Int

Which is, in turn, applied to

    [1,2,3] :: [Int]

The argument in ((+7) <$>) has type Functor f => f Int, whereas [1,2,3] is
[Int], so what can f be?

Answer: []. Yes, [] is a functor, the way Maybe and IO are functors. So f =
[]. It's unusual but only syntatically in the sense that Haskell says to
write Maybe Int but rejects [] Int. You have to write [Int] to mean [] Int.

We say that

    ((+7) <$>) :: Functor f => f Int - f Int

is thus specialized to

    ((+7) <$>) :: [Int] -> [Int]

which is why (+7) <$> [1,2,3] typechecks and runs as it should.

If you run through the above against the rest of the list of things that do
and don't work, I think you'll sew up typed combinators as a fine feather
in your cap. Just remember that [] is a Functor and also an Applicative and
a Monad.

Trying different combinations is so much more satisfying if you lean on the
types to lead you to greater good.

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140408/29f52f8c/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 70, Issue 16
*****************************************

Reply via email to