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:  another list comprehesion error (David Place)
   2. Re:  another list comprehesion error (Roelof Wobben)
   3. Re:  another list comprehesion error (David Place)
   4. Re:  another list comprehesion error (Roelof Wobben)
   5. Re:  another list comprehesion error (Thomas)
   6.  very impure [global] counter (Davi Santos)
   7.  FW:  another list comprehesion error (Roelof Wobben)
   8. Re:  another list comprehesion error (Thomas)


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

Message: 1
Date: Thu, 21 Jul 2011 14:05:14 -0400
From: David Place <d...@vidplace.com>
Subject: Re: [Haskell-beginners] another list comprehesion error
To: Roelof Wobben <rwob...@hotmail.com>
Cc: beginners@haskell.org
Message-ID: <0c14b3f7-7d34-44cb-a7b9-53c4b887a...@vidplace.com>
Content-Type: text/plain; charset="iso-8859-1"

On Jul 21, 2011, at 1:27 PM, Roelof Wobben wrote:

> roelof :: a -> b -> c -> (a,b,c)
> roelof n = [(x y z) | x^2+Y^2=Z^2 <- x<-[1..n], y<- [1..n], z<-[1..n]]
> 
> But I get this error : oefening.hs:2:30: parse error on input `='

Hi, Roelof.

This short amount of code has many syntax and semantic errors.    May I suggest 
that you might benefit from trying to make this function using only normal 
function application and the basic list functions.    I think you will learn 
more about Haskell this way.  Personally, i rarely use list comprehensions.  I 
think they are not very modular.

Also, it might help people to understand your background a little.  This way 
comments could be better focused.  For instance, which other programming 
languages do you know?

____________________
David Place   
Owner, Panpipes Ho! LLC
http://panpipesho.com
d...@vidplace.com


-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110721/820fa873/attachment-0001.htm>

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

Message: 2
Date: Thu, 21 Jul 2011 18:47:05 +0000
From: Roelof Wobben <rwob...@hotmail.com>
Subject: Re: [Haskell-beginners] another list comprehesion error
To: <beginners@haskell.org>
Message-ID: <snt118-w3666816c74339b826c351bae...@phx.gbl>
Content-Type: text/plain; charset="iso-8859-1"




Oke, 

So I can better goto the next chapter or look for a better book.

Im using now Programming in Haskell.

 

My background is that many years I have tried to programm in Delphi.
The last years I have tried C and C++ but these were to difficult for me with 
header files.

I could make this with a for next loop and a if then  if you like it that way ?

 

Roelof
________________________________
> Subject: Re: [Haskell-beginners] another list comprehesion error 
> From: d...@vidplace.com 
> Date: Thu, 21 Jul 2011 14:05:14 -0400 
> CC: beginners@haskell.org 
> To: rwob...@hotmail.com 
> 
> On Jul 21, 2011, at 1:27 PM, Roelof Wobben wrote: 
> 
> roelof :: a -> b -> c -> (a,b,c) 
> roelof n = [(x y z) | x^2+Y^2=Z^2 <- x<-[1..n], y<- [1..n], z<-[1..n]] 
> 
> But I get this error : oefening.hs:2:30: parse error on input `=' 
> 
> Hi, Roelof. 
> 
> This short amount of code has many syntax and semantic errors. May I 
> suggest that you might benefit from trying to make this function using 
> only normal function application and the basic list functions. I 
> think you will learn more about Haskell this way. Personally, i rarely 
> use list comprehensions. I think they are not very modular. 
> 
> Also, it might help people to understand your background a little. 
> This way comments could be better focused. For instance, which other 
> programming languages do you know? 
> 
> ____________________ 
> David Place 
> Owner, Panpipes Ho! LLC 
> http://panpipesho.com 
> d...@vidplace.com<mailto:d...@vidplace.com> 
> 
>                                         


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

Message: 3
Date: Thu, 21 Jul 2011 15:19:54 -0400
From: David Place <d...@vidplace.com>
Subject: Re: [Haskell-beginners] another list comprehesion error
To: Roelof Wobben <rwob...@hotmail.com>
Cc: beginners@haskell.org
Message-ID: <17af5946-abe4-48b7-a84d-5797ad20b...@vidplace.com>
Content-Type: text/plain; charset=us-ascii

On Jul 21, 2011, at 2:47 PM, Roelof Wobben wrote:

> I could make this with a for next loop and a if then  if you like it that way 
> ?

I though it would be trivial, and perhaps it is.  This seems to be a case where 
the list comprehensions give quite a bit of help.

First of all, here is your function with all the syntax and semantic bugs 
fixed.  Please compare it closely to your version.

> roelof :: Int -> [(Int,Int,Int)]
> roelof n = [(x, y, z) | x<-[1..n], y<- [1..n], z<-[1..n],  x^2+y^2 == z^2 ]
> 


Here's a version using only functions without any of the syntactic sugar of 
list comprehensions.

> triples xs ys zs = concatMap (\x -> concatMap (\y -> (map (\z -> (x,y,z)) 
> zs)) ys) xs
> roelof' n = filter (\(x,y,z) -> x^2+y^2 == z^2) $ triples [1..n] [1..n] [1..n]


It's quite unattractive.  Please someone tell me there is a better way.





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

Message: 4
Date: Thu, 21 Jul 2011 19:29:48 +0000
From: Roelof Wobben <rwob...@hotmail.com>
Subject: Re: [Haskell-beginners] another list comprehesion error
To: <d...@vidplace.com>, <beginners@haskell.org>
Message-ID: <snt118-w340511503a22383d8f8784ae...@phx.gbl>
Content-Type: text/plain; charset="iso-8859-1"




----------------------------------------
> Subject: Re: [Haskell-beginners] another list comprehesion error
> From: d...@vidplace.com
> Date: Thu, 21 Jul 2011 15:19:54 -0400
> CC: beginners@haskell.org
> To: rwob...@hotmail.com
>
> On Jul 21, 2011, at 2:47 PM, Roelof Wobben wrote:
>
> > I could make this with a for next loop and a if then if you like it that 
> > way ?
>
> I though it would be trivial, and perhaps it is. This seems to be a case 
> where the list comprehensions give quite a bit of help.
>
> First of all, here is your function with all the syntax and semantic bugs 
> fixed. Please compare it closely to your version.
>
> > roelof :: Int -> [(Int,Int,Int)]
> > roelof n = [(x, y, z) | x<-[1..n], y<- [1..n], z<-[1..n], x^2+y^2 == z^2 ]
> >
>


 

Oke, I see that x<[1..n] part is now in the middle.

That I find quite confusing.

 

When I have made a list compresshion where I must print out a text I look like 
this ;

 

roelof n x = [x | y <- [1..n]]

 

So I thought the syntax of a list compression would be [output | filter <- 
input] 

But the answer is now  [ output | input <- filter] This is very confusing for 
me.

 

 

>
> Here's a version using only functions without any of the syntactic sugar of 
> list comprehensions.
>
> > triples xs ys zs = concatMap (\x -> concatMap (\y -> (map (\z -> (x,y,z)) 
> > zs)) ys) xs
> > roelof' n = filter (\(x,y,z) -> x^2+y^2 == z^2) $ triples [1..n] [1..n] 
> > [1..n]
>
>
> It's quite unattractive. Please someone tell me there is a better way.
>
>


I thought of doing something like this in pseudo code :

 

For teller = 1 to n 

    for teller2 = 1 to n 

         antwoord = (teller1 ^2 + teller^2)^ 0.5

            if antwoord == int(antwoord) then print x,y,z

 

 

 

 

Roelof

  

         

                                          


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

Message: 5
Date: Thu, 21 Jul 2011 23:40:52 +0200
From: Thomas <hask...@phirho.com>
Subject: Re: [Haskell-beginners] another list comprehesion error
To: beginners@haskell.org
Message-ID: <4e289ce4....@phirho.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi!

On 21.07.2011 21:29, Roelof Wobben wrote:
> roelof n x = [x | y<- [1..n]]

Have you tried this?
What do you get for, say "roelof 4 5" ? Why?

Now try this instead:
   roelof' n = [x | x <- [1..n]]
What do you get for "roelof' 4" ?

> So I thought the syntax of a list compression would be [output | filter<- 
> input]
> But the answer is now  [ output | input<- filter] This is very confusing for 
> me.

Hm, in the terminology of the book you're using the list comprehension
    [x | x <- [1..n]] reads
"all x such that x drawn from [1..n]"

So, you have a result (x) and  a generator (x <- [1..n]).
What you call "filter" is called "guard" in the book.
So actually you have:
   [ result | generator(s) ]
or (with guards):
   [ result | generator(s), guards(s) ]

So, note that the comma (,) is a mere separator and the left arrow (<-) 
forms part of the generators (everything else is a guard or an error). 
Especially the term "input" is rather misleading.

Generators are explained in chapter 5.1 (page 38f), guards are explained 
in 5.2 (page 39f). I'm not sure I can explain better than the book. 
Maybe you should experiment with generators first and then advance to 
the guards.

A few suggestions (without guards):
The list of the first 5 natural numbers.
The list of the first 5 odd numbers.
The list of all pairs (a, b) where a,b > 0, a <= b and b <= 5

Now you can try the last two with guards.

HTH,
Thomas








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

Message: 6
Date: Fri, 22 Jul 2011 04:10:55 -0300
From: Davi Santos <dps....@gmail.com>
Subject: [Haskell-beginners] very impure [global] counter
To: beginners@haskell.org
Message-ID:
        <CANWsST9=hq5re_q1wbuxaqsew9nhuhq3xursmvcgtwcy5oe...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hello all,
I have massive (parallel if possible) system calls to an external
non-deterministic program.
Each time it is executed, it creates a file depending on a command line
option 'opt' (input files path, for example).
How can I ensure the file name will be unique? maybe with a global counter?
My temporary solution have been to use a large random number:

-----------
mysteriousExecution :: String -> IO ()
mysteriousExecution opt = do
   number <- rand
   run $ "mysterious-command " ? opt ? " --create-file=" ? number

rand = do
   a ?  getStdRandom (randomR (1,999999999999999999999999999999999)) ?  IO
Int
   let r = take 20 $ randomRs ('a','z') (mkStdGen a) ?  String
   return r
========

I'm trying to avoid additional parameters to 'mysteriousExecution'.
I tried a counter also (to replace rand), but I don't know how could I start
it inside  'mysteriousExecution'.
c ?  IO Counter
c = do
    r ?  newIORef 0            -- start
    return (do
        modifyIORef r (+1)
        readIORef r)

If somebody says everything is wrong, ok.
I understand. 18 years of imperative programming world can damage the brain.

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110722/e0867294/attachment-0001.htm>

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

Message: 7
Date: Fri, 22 Jul 2011 09:13:28 +0000
From: Roelof Wobben <rwob...@hotmail.com>
Subject: [Haskell-beginners] FW:  another list comprehesion error
To: <beginners@haskell.org>
Message-ID: <snt118-w1445a61dd5f8f4268eb880ae...@phx.gbl>
Content-Type: text/plain; charset="iso-8859-1"




----------------------------------------
> From: rwob...@hotmail.com
> To: hask...@phirho.com
> Subject: RE: [Haskell-beginners] another list comprehesion error
> Date: Fri, 22 Jul 2011 06:46:03 +0000
>
>
>
>
> ----------------------------------------
> > Date: Thu, 21 Jul 2011 23:40:52 +0200
> > From: hask...@phirho.com
> > To: beginners@haskell.org
> > Subject: Re: [Haskell-beginners] another list comprehesion error
> >
> > Hi!
> >
> > On 21.07.2011 21:29, Roelof Wobben wrote:
> > > roelof n x = [x | y<- [1..n]]
> >
> > Have you tried this?
> > What do you get for, say "roelof 4 5" ? Why?


 

I get [5,5,5,,5]
And that the right answer according to the exercise

> > Now try this instead:
> > roelof' n = [x | x <- [1..n]]
> > What do you get for "roelof' 4" ?


A error message that a instance of print is missing.



> > A few suggestions (without guards):
> > The list of the first 5 natural numbers.
> > The list of the first 5 odd numbers.
> > The list of all pairs (a, b) where a,b > 0, a <= b and b <= 5
> >
> > Now you can try the last two with guards.


without guards



1) [x | x <- [1..5]]
2) cannot be done without guards and list comprehession generator [ 2,4 ..10] 
does not work
3) cannot be done withut guards and list comprehession because of the a<=b

with guards.


1) has no need for guards.
2) [x | x <-[1..10], even x]
3] [ (a,b) | a<- [1..4], b<- [1..5], a<=b]

Roelof

                                          


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

Message: 8
Date: Fri, 22 Jul 2011 11:31:07 +0200
From: Thomas <hask...@phirho.com>
Subject: Re: [Haskell-beginners] another list comprehesion error
To: beginners@haskell.org
Message-ID: <4e29435b.4000...@phirho.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 22.07.2011 08:46, Roelof Wobben wrote:

>> Now try this instead:
>> roelof' n = [x | x<- [1..n]]
>> What do you get for "roelof' 4" ?
> A error message that a instance of print is missing.

Then you have a typo somewhere...

>> Now you can try the last two with guards.

> 2) cannot be done without guards and list comprehession  generator [ 2,4 
> ..10] does not work

Sure it can:
[ 2*x-1 | x <- [1..5]]
and even easier:
[1,3..10]
or, better still (but not only a list comprehension any more):
take 5 [1, 3..]

> 3) cannot be done withut guards and list comprehession because of the a<=b

This, too, can be done:
[ (b, a) | a <- [1..5], b <- [1..a]]

Regards,
Thomas



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

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


End of Beginners Digest, Vol 37, Issue 44
*****************************************

Reply via email to