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 monad question (David Virebayre)
   2. Re:  list monad question (David Virebayre)
   3. Re:  list monad question (Matthias Guedemann)
   4. Re:  list monad question (Daniel Fischer)
   5. Re:  list monad question (Matthias Guedemann)
   6. Re:  list monad question (Matthias Guedemann)
   7. Re:  list monad question (Matthias Guedemann)
   8.  beginner question (Luca Ciciriello)
   9.  Re: list monad question (Ertugrul Soeylemez)
  10. Re:  Re: list monad question (Matthias Guedemann)


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

Message: 1
Date: Fri, 30 Oct 2009 13:29:20 +0100
From: David Virebayre <dav.vire+hask...@gmail.com>
Subject: Re: [Haskell-beginners] list monad question
To: Matthias Guedemann <matthias.guedem...@ovgu.de>
Cc: beginners <beginners@haskell.org>
Message-ID:
        <4c88418c0910300529h2ef4cd1eib5e2c13e0b3f8...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Fri, Oct 30, 2009 at 1:09 PM, David Virebayre
<dav.vire+hask...@gmail.com> wrote:
> Almost there :

Nevermind, I didn't read the question carefully
Sorry about that :(


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

Message: 2
Date: Fri, 30 Oct 2009 13:36:21 +0100
From: David Virebayre <dav.vire+hask...@gmail.com>
Subject: Re: [Haskell-beginners] list monad question
To: Matthias Guedemann <matthias.guedem...@ovgu.de>
Cc: beginners <beginners@haskell.org>
Message-ID:
        <4c88418c0910300536p75b38a50k629b60aef643e...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Fri, Oct 30, 2009 at 12:44 PM, Matthias Guedemann
<matthias.guedem...@ovgu.de> wrote:

> Now I want to make it capable to create all combinations of length n instead 
> of
> fixed length 3 (that's why list instead of tuple), but I don't really see how.

If I understood what you ask this time, there's a function in
Control.Monad that does it :
allN = replicateM

replicateM 4 [ 1,2,3 ] = [ [ 1,1,1,1],[1,1,1,2], ....

when you write

a <- ls
b <- ls
c <- ls

You perform the monad "action" 3 times, collecting the result in a
then b, then c.
now replicateM performs a monad action n times, collecting the result in a list.
turns out that making a list of the result is exactly what you want.

David.


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

Message: 3
Date: Fri, 30 Oct 2009 13:37:55 +0100
From: Matthias Guedemann <matthias.guedem...@ovgu.de>
Subject: Re: [Haskell-beginners] list monad question
To: David Virebayre <dav.vire+hask...@gmail.com>
Cc: beginners <beginners@haskell.org>
Message-ID: <1256906197-sup-9...@pc44es141.cs.uni-magdeburg.de>
Content-Type: text/plain; charset=UTF-8


Thanks David,

> all3 ls = do
>   a <- ls
>   b <- ls
>   c <- ls
>   return (a,b,c)
> 
> For each element a of list ls , for each element b of the same list
> ls, and for each element c of the same list ls, make a tuple of them.
> return the list of tall the tuples.

But it is a bit more complicated. I changed the result to [a,b,c] in order to
have variable length results. I am now trying to get a  

"allN ls n" function that returns the result for the original problem with
"allN [0..5] 3" and all combinations of the form [a,b] with "allN [0..5] 2".

So basically I want a variable number of name bindings in the list
comprehension. Is this possible in a (simple) way using the list monad? Maybe
like 

allN ls n = foldr (>>=) [] (replicate n ls) >>= return 


regards,

Matthias
-- 
__________________________________________________________
                                            ___  __    __
Dipl. Inf. Matthias Guedemann              / __\/ _\  /__\
Computer Systems in Engineering           / /   \ \  /_\
Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__
Tel.: 0391 / 67-19359                    \____/ \__/\__/
__________________________________________________________


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

Message: 4
Date: Fri, 30 Oct 2009 13:56:34 +0100
From: Daniel Fischer <daniel.is.fisc...@web.de>
Subject: Re: [Haskell-beginners] list monad question
To: beginners@haskell.org
Message-ID: <200910301356.34688.daniel.is.fisc...@web.de>
Content-Type: text/plain;  charset="utf-8"

Am Freitag 30 Oktober 2009 12:44:41 schrieb Matthias Guedemann:
> Hi,
>
> a friend of mine wanted to write function (in Perl) that creates all tuples
> of length 3 of the elements of a given list,
> e.g. [(0,0,0),(0,0,1),(0,0,2),...,(5,5,5)] for the list [0..5]. Trying to
> get better at Haskell, I wrote a small function using the list monad for
> this (tuples replaced with lists)
>
> all3 ls = do
>   a <- ls
>   b <- ls
>   c <- ls
>   return [a,b,c]
>
> Now I want to make it capable to create all combinations of length n
> instead of fixed length 3 (that's why list instead of tuple), but I don't
> really see how. As the do notation translates to
>
> ls >>= \a ->  etc.
>
> I thought it should be possible to have some sort of "foldr (>>=)" over a
> list of length n, but I can't figure out how to collect the variable number
> of results in a list for the "return".

Recursive version first:

combinations 0 _ = [[]]    -- whatever the list, there's one way to pick 0 
elements
combinations n xs = do
    h <- xs
    t <- combinations (n-1) xs
    return (h:t)

(check for n < 0 omitted)

So, what are we doing? We have one list of a (xs) and one list of [a] (ys = 
combinations 
(n-1) xs) and cons each element of xs to each element of ys:

f xs ys = [h:t | h <- xs, t <- ys]
        = concat [[h:t | t <- ys] | h <- xs]
        = concat [map (h:) ys | h <- xs]
        = concat (map (\h -> map (h:) ys) xs)
        = xs >>= \h -> map (h:) ys

That gives

combinations n xs = foldr f [[]] (replicate n xs)

pointfree, for extra goodness:

-- pointfree f inline
combinations n xs = foldr ((. (. (:)) . flip map) . (>>=)) [[]] (replicate n xs)
-- eliminate xs
combinations n = foldr ((. (. (:)) . flip map) . (>>=)) [[]] . replicate n
-- completely pointfree
combinations = (foldr ((. (. (:)) . flip map) . (>>=)) [[]]  .) . replicate

>
> Any hints for that?
>
> best regards
> Matthias



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

Message: 5
Date: Fri, 30 Oct 2009 13:56:40 +0100
From: Matthias Guedemann <matthias.guedem...@ovgu.de>
Subject: Re: [Haskell-beginners] list monad question
To: Jan Jakubuv <jaku...@gmail.com>
Cc: beginners <beginners@haskell.org>
Message-ID: <1256907306-sup-6...@pc44es141.cs.uni-magdeburg.de>
Content-Type: text/plain; charset=UTF-8

> How about a recursive function like this:
> 
>     alln 1 ls = map (:[]) ls
>     alln n ls = do
>         a <- ls
>         as <- alln (n-1) ls
>         return (a:as)
> 
> Note that `ls :: [t]` and `all (n-1) ls :: [[t]]` has different types but
> it's okay because both are in the list monad. 
> 
> Also, it can be done with list comprehensions:
> 
>     alln' 1 ls = [[a] | a<-ls] 
>     alln' n ls = [a:as | a<-ls, as<-alln' (n-1) ls]


Works great, thanks a lot

Matthias


-- 
__________________________________________________________
                                            ___  __    __
Dipl. Inf. Matthias Guedemann              / __\/ _\  /__\
Computer Systems in Engineering           / /   \ \  /_\
Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__
Tel.: 0391 / 67-19359                    \____/ \__/\__/
__________________________________________________________


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

Message: 6
Date: Fri, 30 Oct 2009 14:01:20 +0100
From: Matthias Guedemann <matthias.guedem...@ovgu.de>
Subject: Re: [Haskell-beginners] list monad question
To: David Virebayre <dav.vire+hask...@gmail.com>
Cc: beginners <beginners@haskell.org>
Message-ID: <1256907417-sup-...@pc44es141.cs.uni-magdeburg.de>
Content-Type: text/plain; charset=UTF-8


Hallo David,

> If I understood what you ask this time, there's a function in
> Control.Monad that does it :
> allN = replicateM
> 
> replicateM 4 [ 1,2,3 ] = [ [ 1,1,1,1],[1,1,1,2], ....

that is exactly what I wanted, thanks a lot. Next time I will state the question
more clearly :-)

> when you write
> 
> a <- ls
> b <- ls
> c <- ls
> 
> You perform the monad "action" 3 times, collecting the result in a
> then b, then c.
> now replicateM performs a monad action n times, collecting the result in a 
> list.
> turns out that making a list of the result is exactly what you want.

Maybe I should have a closer look at Control.Monad, a lot seems to be there
already.

best regards,
Matthias

-- 
__________________________________________________________
                                            ___  __    __
Dipl. Inf. Matthias Guedemann              / __\/ _\  /__\
Computer Systems in Engineering           / /   \ \  /_\
Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__
Tel.: 0391 / 67-19359                    \____/ \__/\__/
__________________________________________________________


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

Message: 7
Date: Fri, 30 Oct 2009 14:32:35 +0100
From: Matthias Guedemann <matthias.guedem...@ovgu.de>
Subject: Re: [Haskell-beginners] list monad question
To: Daniel Fischer <daniel.is.fisc...@web.de>
Cc: beginners <beginners@haskell.org>
Message-ID: <1256909341-sup-3...@pc44es141.cs.uni-magdeburg.de>
Content-Type: text/plain; charset=UTF-8



Hi Daniel,

> That gives
> 
> combinations n xs = foldr f [[]] (replicate n xs)
> 
> pointfree, for extra goodness:
> 
> -- pointfree f inline
> combinations n xs = foldr ((. (. (:)) . flip map) . (>>=)) [[]] (replicate n 
> xs)
> -- eliminate xs
> combinations n = foldr ((. (. (:)) . flip map) . (>>=)) [[]] . replicate n
> -- completely pointfree
> combinations = (foldr ((. (. (:)) . flip map) . (>>=)) [[]]  .) . replicate

thank you, looks rather strange to me but works well.

regards

-- 
__________________________________________________________
                                            ___  __    __
Dipl. Inf. Matthias Guedemann              / __\/ _\  /__\
Computer Systems in Engineering           / /   \ \  /_\
Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__
Tel.: 0391 / 67-19359                    \____/ \__/\__/
__________________________________________________________


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

Message: 8
Date: Fri, 30 Oct 2009 13:40:13 +0000
From: Luca Ciciriello <luca_cicirie...@hotmail.com>
Subject: [Haskell-beginners] beginner question
To: <beginners@haskell.org>
Message-ID: <snt128-w13c9bf74147683b0cc8c0c9a...@phx.gbl>
Content-Type: text/plain; charset="iso-8859-1"


Hi all.

Just a very basic question.

 

I need to write a function str2lsts :: String -> [[String]] in order to 
transorm a string like:

 

"\"1\",\"cat\",\"dog\"ยง\"2\",\"duck\",\"goose\""

 

in the list of lists:

 

[["1","cat","dog"],["2","duck","goose"]]

 

I've tried to mix recursion, pattern matching and list comprehension, but the 
obtained result was embarrassing complex (> 20 lines of awful code). I think 
that a more simple solution certainly exists. 


 

Thanks in advance for any idea.

 

Luca
                                          
_________________________________________________________________
Download Messenger onto your mobile for free
http://clk.atdmt.com/UKM/go/174426567/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091030/4a0586e3/attachment-0001.html

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

Message: 9
Date: Fri, 30 Oct 2009 15:01:29 +0100
From: Ertugrul Soeylemez <e...@ertes.de>
Subject: [Haskell-beginners] Re: list monad question
To: beginners@haskell.org
Message-ID: <20091030150129.1f758...@tritium.xx>
Content-Type: text/plain; charset=US-ASCII

Hello Matthias,

you may want to have a look at section 11 of my monads tutorial [1],
which contains monadic library functions like replicateM together with
examples and detailed explanations.

[1] http://ertes.de/articles/monads.html#section-11


Greets,
Ertugrul.


Matthias Guedemann <matthias.guedem...@ovgu.de> wrote:

> 
> Hi,
> 
> a friend of mine wanted to write function (in Perl) that creates all tuples of
> length 3 of the elements of a given list,
> e.g. [(0,0,0),(0,0,1),(0,0,2),...,(5,5,5)] for the list [0..5]. Trying to get
> better at Haskell, I wrote a small function using the list monad for this 
> (tuples
> replaced with lists)
> 
> all3 ls = do
>   a <- ls
>   b <- ls
>   c <- ls
>   return [a,b,c]
> 
> Now I want to make it capable to create all combinations of length n instead 
> of
> fixed length 3 (that's why list instead of tuple), but I don't really see how.
> As the do notation translates to 
> 
> ls >>= \a ->  etc. 
> 
> I thought it should be possible to have some sort of "foldr (>>=)" over a list
> of length n, but I can't figure out how to collect the variable number of
> results in a list for the "return".
> 
> Any hints for that?
> 
> best regards
> Matthias
> 
> 



-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/




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

Message: 10
Date: Fri, 30 Oct 2009 15:39:25 +0100
From: Matthias Guedemann <matthias.guedem...@ovgu.de>
Subject: Re: [Haskell-beginners] Re: list monad question
To: beginners <beginners@haskell.org>
Message-ID: <1256912868-sup-...@pc44es141.cs.uni-magdeburg.de>
Content-Type: text/plain; charset=UTF-8


Hello Ertugrul,

this looks interesting. I read Brent Yorgeys Typeclassopedia and also the work
you cite in your tutorial. 
So I am trying to learn about Applicative, Monoids, Monads etc. by using
them. Your description of the library functions comes handy, as reading the
source directly does not really help if you're still struggling with
understanding the concepts. And on the other hand, due to the abstract nature of
monads, a lot of functionality should be "hidden" in the library (just like my
rewrite of replicateM)

thank you very much, will read it on the weekend

regards
Matthias




Excerpts from Ertugrul Soeylemez's message of Fr Okt 30 15:01:29 +0100 2009:
> Hello Matthias,
> 
> you may want to have a look at section 11 of my monads tutorial [1],
> which contains monadic library functions like replicateM together with
> examples and detailed explanations.
> 
> [1] http://ertes.de/articles/monads.html#section-11
> 
> 
> Greets,
> Ertugrul.
> 
> Matthias Guedemann <matthias.guedem...@ovgu.de> wrote:
> 
> > 
> > Hi,
> > 
> > a friend of mine wanted to write function (in Perl) that creates all tuples 
> > of
> > length 3 of the elements of a given list,
> > e.g. [(0,0,0),(0,0,1),(0,0,2),...,(5,5,5)] for the list [0..5]. Trying to 
> > get
> > better at Haskell, I wrote a small function using the list monad for this 
> > (tuples
> > replaced with lists)
> > 
> > all3 ls = do
> >   a <- ls
> >   b <- ls
> >   c <- ls
> >   return [a,b,c]
> > 
> > Now I want to make it capable to create all combinations of length n 
> > instead of
> > fixed length 3 (that's why list instead of tuple), but I don't really see 
> > how.
> > As the do notation translates to 
> > 
> > ls >>= \a ->  etc. 
> > 
> > I thought it should be possible to have some sort of "foldr (>>=)" over a 
> > list
> > of length n, but I can't figure out how to collect the variable number of
> > results in a list for the "return".
> > 
> > Any hints for that?
> > 
> > best regards
> > Matthias
> > 
> > 
> 
-- 
__________________________________________________________
                                            ___  __    __
Dipl. Inf. Matthias Guedemann              / __\/ _\  /__\
Computer Systems in Engineering           / /   \ \  /_\
Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__
Tel.: 0391 / 67-19359                    \____/ \__/\__/
__________________________________________________________


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

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


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

Reply via email to