Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1. Re:  List Function (Edward Z. Yang)
   2. Re:  List Function (Joe Fredette)
   3. Re:  List Function (Edward Z. Yang)
   4. Re:  List Function (Joe Fredette)
   5.  putting in a good word for "Haskell: The Craft   of Functional
      Programming" (Michael P Mossey)
   6.  P.S. "Haskell: Craft of Functional Programming"
      (Michael P Mossey)
   7. Re:  List Function (Brent Yorgey)
   8. Re:  List Function (Edward Z. Yang)
   9. Re:  Need Help (kaushal Pathak)


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

Message: 1
Date: Wed, 29 Apr 2009 22:38:40 -0400
From: "Edward Z. Yang" <ezy...@mit.edu>
Subject: Re: [Haskell-beginners] List Function
To: beginners <beginners@haskell.org>
Message-ID: <1241058889-sup-1...@javelin>
Content-Type: text/plain; charset=UTF-8

Excerpts from Nathan Holden's message of Wed Apr 29 22:25:43 -0400 2009:
> Sending two lists, [1,2,3] and [2,3,4] it would return
> [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]. I managed to code
> my way into returning a list of lists, which works. But it seemed like a
> very basic list/matrix function, so I honestly believe that the Haskell
> designers probably would've put it in.

Did you mean [1,2,3] and [4,5,6]?

Cheers,
Edward


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

Message: 2
Date: Wed, 29 Apr 2009 22:39:18 -0400
From: Joe Fredette <jfred...@gmail.com>
Subject: Re: [Haskell-beginners] List Function
To: Nathan Holden <nathanmhol...@gmail.com>
Cc: beginners@haskell.org
Message-ID: <49f90f56.9030...@gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

So, two ways you could do it.

I assume your current function looks like:

    toPairs :: [a] -> [a] -> [[a]]
    toPairs [] _ = []
    toPairs (x:xs) ls = (zip (\y -> (x,y)) ls) : (toPairs xs ls)

This returns something like:

    toPairs [1,2] [3,4] ===> [[(1,3),(1,4)],[(2,3),(2,4)]]

We want a function that takes this list-of-lists to a list. We can 
certainly see the type as:

    foo :: [[a]] -> [a]

`foo` would have to take all the elements of each list, and put it in a 
new one. We know of a function which combines the elements of two lists, 
(++)
so really, our foo function would do:

foo [] = []
foo (x:xs) = x ++ foo xs

concatenating all of the lists together in order. But this is just a 
fold, we can call foo:

foo = foldr (++) -- or foldl would work too.

foldr is just the pattern above, abstracted away to a convenient function.

Of course, this function comes up all the time, so we have one in the 
prelude, it's called. `concat`. and if you look up the source on hoogle, 
I believe you'll find the above.

Nathan Holden wrote:
> I don't know what you'd call it. Is there a function in any of the 
> basic functions that does this something like this:
>
> Sending two lists, [1,2,3] and [2,3,4] it would return 
> [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]. I managed to 
> code my way into returning a list of lists, which works. But it seemed 
> like a very basic list/matrix function, so I honestly believe that the 
> Haskell designers probably would've put it in.
> ------------------------------------------------------------------------
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>   
-------------- next part --------------
A non-text attachment was scrubbed...
Name: jfredett.vcf
Type: text/x-vcard
Size: 296 bytes
Desc: not available
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20090429/e6863019/jfredett-0001.vcf

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

Message: 3
Date: Wed, 29 Apr 2009 22:41:15 -0400
From: "Edward Z. Yang" <ezy...@mit.edu>
Subject: Re: [Haskell-beginners] List Function
To: beginners <beginners@haskell.org>
Message-ID: <1241059179-sup-7...@javelin>
Content-Type: text/plain; charset=UTF-8

Excerpts from Edward Z. Yang's message of Wed Apr 29 22:38:40 -0400 2009:
> Excerpts from Nathan Holden's message of Wed Apr 29 22:25:43 -0400 2009:
> > Sending two lists, [1,2,3] and [2,3,4] it would return
> > [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]. I managed to code
> > my way into returning a list of lists, which works. But it seemed like a
> > very basic list/matrix function, so I honestly believe that the Haskell
> > designers probably would've put it in.
> 
> Did you mean [1,2,3] and [4,5,6]?

To elaborate, a list comprehension is what you want, if you want this function
to do what I think you want it to do.

Prelude> let f as bs = [(a,b) | a <- as, b <- bs]
Prelude> f [1,2,3] [4,5,6]
[(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]

Cheers,
Edward


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

Message: 4
Date: Wed, 29 Apr 2009 22:48:42 -0400
From: Joe Fredette <jfred...@gmail.com>
Subject: Re: [Haskell-beginners] List Function
To: Nathan Holden <nathanmhol...@gmail.com>
Cc: beginners@haskell.org
Message-ID: <49f9118a.1080...@gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Occurs to me I said two ways, and only gave one, the one I gave was the 
tougher way. The easiest way is to use a list comprehension, viz:

    toPairs xs ys = [(x,y) | x <- xs, y <- ys]

This is nice and succint, you can think of it (assuming you're familiar 
with it) as set-builder notation. The '[' and ']' brackets are the set 
brackets '{' and '}'. '<-' is
roughly like the element symbol. You can even add constraints (called 
guards), like so:

   foo xs ys = [x / y | x <- xs, y <- ys, y /= 0]



Joe Fredette wrote:
> So, two ways you could do it.
>
> I assume your current function looks like:
>
>    toPairs :: [a] -> [a] -> [[a]]
>    toPairs [] _ = []
>    toPairs (x:xs) ls = (zip (\y -> (x,y)) ls) : (toPairs xs ls)
>
> This returns something like:
>
>    toPairs [1,2] [3,4] ===> [[(1,3),(1,4)],[(2,3),(2,4)]]
>
> We want a function that takes this list-of-lists to a list. We can 
> certainly see the type as:
>
>    foo :: [[a]] -> [a]
>
> `foo` would have to take all the elements of each list, and put it in 
> a new one. We know of a function which combines the elements of two 
> lists, (++)
> so really, our foo function would do:
>
> foo [] = []
> foo (x:xs) = x ++ foo xs
>
> concatenating all of the lists together in order. But this is just a 
> fold, we can call foo:
>
> foo = foldr (++) -- or foldl would work too.
>
> foldr is just the pattern above, abstracted away to a convenient 
> function.
>
> Of course, this function comes up all the time, so we have one in the 
> prelude, it's called. `concat`. and if you look up the source on 
> hoogle, I believe you'll find the above.
>
> Nathan Holden wrote:
>> I don't know what you'd call it. Is there a function in any of the 
>> basic functions that does this something like this:
>>
>> Sending two lists, [1,2,3] and [2,3,4] it would return 
>> [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]. I managed to 
>> code my way into returning a list of lists, which works. But it 
>> seemed like a very basic list/matrix function, so I honestly believe 
>> that the Haskell designers probably would've put it in.
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners@haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>   
-------------- next part --------------
A non-text attachment was scrubbed...
Name: jfredett.vcf
Type: text/x-vcard
Size: 296 bytes
Desc: not available
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20090429/6e12a534/jfredett-0001.vcf

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

Message: 5
Date: Wed, 29 Apr 2009 20:18:27 -0700
From: Michael P Mossey <m...@alumni.caltech.edu>
Subject: [Haskell-beginners] putting in a good word for "Haskell: The
        Craft   of Functional Programming"
To: beginners <beginners@haskell.org>
Message-ID: <49f91883.30...@alumni.caltech.edu>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

I'm working through "Haskell: the Craft of Functional 
Programming" (by Simon Thompson) in depth, and I want to put in a 
good word for it. His exercises are numerous and they seem to 
lead me from simpler ideas to more complex ideas in a natural 
way. As part of learning Haskell, I actually started with other 
books and tutorials and skipped around in them, reading some of 
the later chapters before the earlier ones. I like that 
sometimes. But I'm finding that Thompson's book is helpful for 
getting an intuition for the language, as opposed to just reading 
about its features, so I'm working it in order.

My other books include "Real World Haskell" and "School of 
Expression." The wonderful thing about RWH is that it's available 
online (I do own a hardcopy) where people comment on the text and 
exercises.

-Mike



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

Message: 6
Date: Wed, 29 Apr 2009 20:20:23 -0700
From: Michael P Mossey <m...@alumni.caltech.edu>
Subject: [Haskell-beginners] P.S. "Haskell: Craft of Functional
        Programming"
To: beginners@haskell.org
Message-ID: <49f918f7.5020...@alumni.caltech.edu>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Oh, and I got HCFP at the local Border's. Apparently it's selling 
well enough to include it in a general bookstore. Look out 
Stephen King---Simon Thompson is coming!


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

Message: 7
Date: Wed, 29 Apr 2009 23:41:03 -0400
From: Brent Yorgey <byor...@seas.upenn.edu>
Subject: Re: [Haskell-beginners] List Function
To: beginners@haskell.org
Message-ID: <20090430034103.ga17...@seas.upenn.edu>
Content-Type: text/plain; charset=us-ascii

On Wed, Apr 29, 2009 at 10:25:43PM -0400, Nathan Holden wrote:
> I don't know what you'd call it. Is there a function in any of the basic
> functions that does this something like this:
> 
> Sending two lists, [1,2,3] and [2,3,4] it would return
> [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]. I managed to code
> my way into returning a list of lists, which works. But it seemed like a
> very basic list/matrix function, so I honestly believe that the Haskell
> designers probably would've put it in.

Others' answers are probably more helpful for learning, but I also
wanted to point out that there is a Prelude function that does
something close to this (assuming you meant [4,5,6]), namely,
'sequence'.

  Prelude> sequence [ [1,2,3], [4,5,6] ]
  [[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]
  Prelude> sequence [ [1,2], [3,4,5], [6,7] ]
  
[[1,3,6],[1,3,7],[1,4,6],[1,4,7],[1,5,6],[1,5,7],[2,3,6],[2,3,7],[2,4,6],[2,4,7],[2,5,6],[2,5,7]]

However, explaining how/why sequence does this requires understanding
the list monad, which you may or may not want to tackle at this point.

If you import 'Control.Applicative' you can even do exactly what you
wanted, with pairs and all:

  Prelude Control.Applicative> liftA2 (,) [1,2,3] [4,5,6]
  [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]

Here, liftA2 is applying the function (,) to every possible pair of
values from the two lists.

-Brent


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

Message: 8
Date: Wed, 29 Apr 2009 23:57:08 -0400
From: "Edward Z. Yang" <ezy...@mit.edu>
Subject: Re: [Haskell-beginners] List Function
To: beginners <beginners@haskell.org>
Message-ID: <1241063266-sup-1...@javelin>
Content-Type: text/plain; charset=UTF-8

Excerpts from Brent Yorgey's message of Wed Apr 29 23:41:03 -0400 2009:
> Others' answers are probably more helpful for learning, but I also
> wanted to point out that there is a Prelude function that does
> something close to this (assuming you meant [4,5,6]), namely,
> 'sequence'.
> 
>   Prelude> sequence [ [1,2,3], [4,5,6] ]
>   [[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]
>   Prelude> sequence [ [1,2], [3,4,5], [6,7] ]
>  
> [[1,3,6],[1,3,7],[1,4,6],[1,4,7],[1,5,6],[1,5,7],[2,3,6],[2,3,7],[2,4,6],[2,4,7]
> ,[2,5,6],[2,5,7]]
> 
> However, explaining how/why sequence does this requires understanding
> the list monad, which you may or may not want to tackle at this point.

Interesting! According to Hoogle, the sequence function evaluates each
monad and then collects the results. I know evaluating a list monad returns
a list, but what do they mean by "collect"?

> If you import 'Control.Applicative' you can even do exactly what you
> wanted, with pairs and all:
> 
>   Prelude Control.Applicative> liftA2 (,) [1,2,3] [4,5,6]
>   [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]
> 
> Here, liftA2 is applying the function (,) to every possible pair of
> values from the two lists.

I'm not even going to try to understand that now.

Cheers,
Edward


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

Message: 9
Date: Wed, 29 Apr 2009 18:35:24 +0530
From: kaushal Pathak <kausha...@gmail.com>
Subject: Re: [Haskell-beginners] Need Help
To: Rahul Kapoor <r...@trie.org>, beginners@haskell.org
Message-ID:
        <6d9efa350904290605r4361a47do3caba24eee96...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Here is another one liner I am stuck at(Find permuation of string), here is
my one line code :

permute myStr = foldr(\x acc -> (zipWith (\x1 y1 -> x1 ++ [x] ++ y1) (inits
acc) (tails acc))) [] myStr

and here is 5 line error ;-(

    Occurs check: cannot construct the infinite type: a = [a]
      Expected type: [a]
      Inferred type: [[a]]
    In the expression:
        (zipWith (\ x1 y1 -> x1 ++ x ++ y1) (inits acc) (tails acc))
    In the first argument of `foldr', namely
        `(\ x acc
              -> (zipWith (\ x1 y1 -> x1 ++ x ++ y1) (inits acc) (tails
acc)))'


Will really appreciate your help in moving ahead

Regards
Kaushal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090429/39380333/attachment.htm

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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 10, Issue 33
*****************************************

Reply via email to