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: folds again -- myCycle (7stud)
   2. Re:  Re: folds again -- myCycle (Daniel Fischer)
   3.  Innovating control structures (Girish Bhat)
   4.  Re: Innovating control structures (Girish Bhat)
   5.  Re: folds again -- myCycle (7stud)
   6.  infix functions with 3 args (7stud)
   7. Re:  infix functions with 3 args (Thomas Davie)
   8.  Problems when trying to solve yaht exercise 3.10 (ZelluX)


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

Message: 1
Date: Thu, 19 Mar 2009 19:32:19 +0000 (UTC)
From: 7stud <bbxx789_0...@yahoo.com>
Subject: [Haskell-beginners] Re: folds again -- myCycle
To: beginners@haskell.org
Message-ID: <loom.20090319t190853-...@post.gmane.org>
Content-Type: text/plain; charset=us-ascii

Will Ness <will_n48 <at> yahoo.com> writes:
> In our case, having 
> The access in (head ys) gets traslated in
> 
> head ys = head (ys ++ [1,2,3]) = head ((ys ++ [1,2,3]) ++ [1,2,3])
> 
> but for the other defintion 
> 
> let zs = [1, 2, 3] ++ zs
> 
> it's
> 
> head zs = head([1, 2, 3] ++ zs) = head( (1:[2,3]) ++ zs) 
> = head( 1:([2,3]++zs) ) = 1
> 
> according to the defintion of (++),
> 
> (x:xs)++ys = x:(xs++ys)
> 
> Were we to use the foldr definition, it'll get rewritten just the same, using 
> the foldr definition (as long as it's not the left-recursive defintion):
> 
> foldr f z (a:as) = a `f` foldr f z as
> 
> let ws = foldr (:) [1,2,3] ws
> 
> head ws = head (foldr (:) [1,2,3] ws) 
> = head (foldr (:) [1,2,3] (foldr (:) [1,2,3] ws))
> 
> because 'ws' is getting matched against (a:as) pattern in foldr definition, 
> so 
> is immediately needed, causing INFINITE looping. BUT
>

I'm confused by your statement that ws is getting matched against the
(a:as) pattern in the foldr definition, and therefore it is immediately 
evaluated.   I didn't think the let expression:

> let ws = foldr (:) [1,2,3] ws

would cause infinite looping.

Wouldn't it be the head pattern that is being matched, and therefore head
triggers the evaluation?  Although, I'm not seeing a pattern match here:

>head (x:xs) = x
>head (foldr (:) [1,2,3] (foldr (:) [1,2,3] ws))


..
...
...
..
..
,,,
...
....






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

Message: 2
Date: Thu, 19 Mar 2009 21:01:30 +0100
From: Daniel Fischer <daniel.is.fisc...@web.de>
Subject: Re: [Haskell-beginners] Re: folds again -- myCycle
To: 7stud <bbxx789_0...@yahoo.com>, beginners@haskell.org
Message-ID: <200903192101.30673.daniel.is.fisc...@web.de>
Content-Type: text/plain;  charset="iso-8859-1"

Am Donnerstag, 19. März 2009 20:32 schrieb 7stud:
> Will Ness <will_n48 <at> yahoo.com> writes:
> > In our case, having
> > The access in (head ys) gets traslated in
> >
> > head ys = head (ys ++ [1,2,3]) = head ((ys ++ [1,2,3]) ++ [1,2,3])
> >
> > but for the other defintion
> >
> > let zs = [1, 2, 3] ++ zs
> >
> > it's
> >
> > head zs = head([1, 2, 3] ++ zs) = head( (1:[2,3]) ++ zs)
> > = head( 1:([2,3]++zs) ) = 1
> >
> > according to the defintion of (++),
> >
> > (x:xs)++ys = x:(xs++ys)
> >
> > Were we to use the foldr definition, it'll get rewritten just the same,
> > using the foldr definition (as long as it's not the left-recursive
> > defintion):
> >
> > foldr f z (a:as) = a `f` foldr f z as
> >
> > let ws = foldr (:) [1,2,3] ws
> >
> > head ws = head (foldr (:) [1,2,3] ws)
> > = head (foldr (:) [1,2,3] (foldr (:) [1,2,3] ws))
> >
> > because 'ws' is getting matched against (a:as) pattern in foldr
> > definition, so is immediately needed, causing INFINITE looping. BUT
>
> I'm confused by your statement that ws is getting matched against the
> (a:as) pattern in the foldr definition, and therefore it is immediately
>
> evaluated.   I didn't think the let expression:
> > let ws = foldr (:) [1,2,3] ws
>
> would cause infinite looping.

Note this is again the left recursive bad definition. As long as we don't want 
to know anything about ws, the definition

ws = foldr (:) [1,2,3] ws

sleeps peacefully.

>
> Wouldn't it be the head pattern that is being matched, and therefore head
>
> triggers the evaluation?

If we need to know about the structure of ws, for example if we query

head ws

the evaluation of ws is triggered. In the case of head, we want to know if ws 
matches the pattern (_:_), in which case head returns the head of ws, or not, 
in which case head throws an exception.

So the implementation tries to match ws with the pattern (_:_). To see if it 
matches, it must evaluate ws, first replacing ws by the right hand side of 
its definition, foldr (:) [1,2,3] ws. Doesn't yet say if ws matches (_:_), so 
the foldr must be evaluated.

foldr (:) [1,2,3] [] ~> [1,2,3]
foldr (:) [1,2,3] (y:ys) = y:foldr (:) [1,2,3] ys

To know which equation to use, the implementation tries to match ws with [].
Unfortunately, there is no information about the structure of ws available 
yet. So ws is replaced by the right hand side of its definition to find out 
whether it matches [], leading to

head (foldr (:) [1,2,3] (foldr (:) [1,2,3] ws)).

Dang, to see whether the inner foldr returns an empty list or not, we need to 
know whether ws is empty or not, so we must replace ws in the inner foldr 
with the right hand side of its definition...

>  Although, I'm not seeing a pattern match here:
> >head (x:xs) = x
         ^^^^^^ the pattern
> >head (foldr (:) [1,2,3] (foldr (:) [1,2,3] ws))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
the expression which is matched against the pattern



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

Message: 3
Date: Fri, 20 Mar 2009 02:33:43 +0530
From: Girish Bhat <girishbhat6...@gmail.com>
Subject: [Haskell-beginners] Innovating control structures
To: beginners@haskell.org
Message-ID:
        <cdef83d60903191403h7a57ba96wd1aa63007d742...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Ok, I keep hearing about functional programming allowing you to
innovate control structures and certainly monads have a role to play
there.
I was wondering if you guys would point me to particularly elegant or
pleasing idioms.

For example,  I seem to be pleased to be re-acquainted with the
recursive for loop :)


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

Message: 4
Date: Fri, 20 Mar 2009 12:59:28 +0530
From: Girish Bhat <girishbhat6...@gmail.com>
Subject: [Haskell-beginners] Re: Innovating control structures
To: beginners@haskell.org
Message-ID:
        <cdef83d60903200029k40f428b8u8dcdc3b3ecda1...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Ok, I just got a bit panaroid that I didn't specify that I do know
about fold, map etc :)

On 3/20/09, Girish Bhat <girishbhat6...@gmail.com> wrote:
> Ok, I keep hearing about functional programming allowing you to
> innovate control structures and certainly monads have a role to play
> there.
> I was wondering if you guys would point me to particularly elegant or
> pleasing idioms.
>
> For example,  I seem to be pleased to be re-acquainted with the
> recursive for loop :)
>


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

Message: 5
Date: Fri, 20 Mar 2009 08:21:07 +0000 (UTC)
From: 7stud <bbxx789_0...@yahoo.com>
Subject: [Haskell-beginners] Re: folds again -- myCycle
To: beginners@haskell.org
Message-ID: <loom.20090320t080001-...@post.gmane.org>
Content-Type: text/plain; charset=utf-8

Daniel Fischer <daniel.is.fischer <at> web.de> writes:
> Am Donnerstag, 19. März 2009 20:32 schrieb 7stud:
> > Will Ness <will_n48 <at> yahoo.com> writes:
> > > let ws = foldr (:) [1,2,3] ws
> > >
> > > head ws = head (foldr (:) [1,2,3] ws)
> > > = head (foldr (:) [1,2,3] (foldr (:) [1,2,3] ws))
> > >
> > > because 'ws' is getting matched against (a:as) pattern in foldr
> > > definition, so is immediately needed, causing INFINITE looping. BUT
> >
> > I'm confused by your statement that ws is getting matched against the
> > (a:as) pattern in the foldr definition, and therefore it is immediately
> >
> > evaluated.   I didn't think the let expression:
> > > let ws = foldr (:) [1,2,3] ws
> >
> > would cause infinite looping.
> 
> Note this is again the left recursive bad definition. As long as we don't 
> want 
> to know anything about ws, the definition
> 
> ws = foldr (:) [1,2,3] ws
> 
> sleeps peacefully.
>

Check...................................................
..............................................................
............................................................
.............................................................
 

> > Wouldn't it be the head pattern that is being matched, and therefore head
> >
> > triggers the evaluation?
> 
> If we need to know about the structure of ws, for example if we query
> 
> head ws
> 
> the evaluation of ws is triggered. In the case of head, we want to know if ws 
> matches the pattern (_:_), in which case head returns the head of ws, or not, 
> in which case head throws an exception.
> 
> So the implementation tries to match ws with the pattern (_:_). To see if it 
> matches, it must evaluate ws, first replacing ws by the right hand side of 
> its definition, foldr (:) [1,2,3] ws. Doesn't yet say if ws matches (_:_), so 
> the foldr must be evaluated.
.........................
.........................
.........................
.........................
.........................

  foldr  _    zero  []  =  zero  (from def of foldr)
> foldr (:) [1,2,3] [] ~> [1,2,3]   
> foldr (:) [1,2,3] (y:ys) =   y  :  foldr  (:) [1,2,3] ys
                           =  (:) y (foldr  (:) [1,2,3] ys)
  foldr step  zero  (x:xs) = step x (foldr  step  zero  xs)
                  (from definition of foldr) 

...........................
...........................
..........................
..........................
> To know which equation to use, the implementation tries to match ws with [].
> Unfortunately, there is no information about the structure of ws available 
> yet. So ws is replaced by the right hand side of its definition to find out 
> whether it matches [], leading to
> 
> head (foldr (:) [1,2,3] (foldr (:) [1,2,3] ws)).
> 

> Dang, to see whether the inner foldr returns an empty list or not 

?? 

I think you meant to say something like, "to see whether ws is an empty
list and therefore foldr returns [1, 2, 3]:

  foldr (:) [1,2,3] ws
> foldr (:) [1,2,3] [] = [1,2,3]   

we need to know whether ws is empty or not."

> so we must replace ws in the inner foldr 
> with the right hand side of its definition...
> 
> >  Although, I'm not seeing a pattern match here:
> > >head (x:xs) = x
>          ^^^^^^ the pattern
> > >head (foldr (:) [1,2,3] (foldr (:) [1,2,3] ws))
>             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> the expression which is *trying*(?) to be matched against the pattern
> 
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
....
...
...
...
...







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

Message: 6
Date: Fri, 20 Mar 2009 11:05:49 +0000 (UTC)
From: 7stud <bbxx789_0...@yahoo.com>
Subject: [Haskell-beginners] infix functions with 3 args
To: beginners@haskell.org
Message-ID: <loom.20090320t105633-...@post.gmane.org>
Content-Type: text/plain; charset=us-ascii

On p. 76 of RWH, it says:

"If a function or constructor takes two or more arguments, we have 
the option of using it in infix 
form, where we place it between its first and second arguments."

Here is my code:

func2 x y = x + y

func3 x y z  = x + y + z

Here are the results:

*Main> func2 10 20
30
*Main> 10 `func2` 20
30
*Main> func3 10 20 30
60
*Main> 10 `func3` 20 30

<interactive>:1:11:
    No instance for (Num (t1 -> t))
      arising from the literal `20' at <interactive>:1:11-15
    Possible fix: add an instance declaration for (Num (t1 -> t))
    In the second argument of `func3', namely `20 30'
    In the expression: 10 `func3` 20 30
    In the definition of `it': it = 10 `func3` 20 30

How do you get a function to work using infix notation when it has
3 arguments?






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

Message: 7
Date: Fri, 20 Mar 2009 12:07:52 +0100
From: Thomas Davie <tom.da...@gmail.com>
Subject: Re: [Haskell-beginners] infix functions with 3 args
To: 7stud <bbxx789_0...@yahoo.com>
Cc: beginners@haskell.org
Message-ID: <5dfe0a56-c8ff-4d6b-9f1a-0fc807024...@gmail.com>
Content-Type: text/plain; charset=US-ASCII; format=flowed


On 20 Mar 2009, at 12:05, 7stud wrote:

> On p. 76 of RWH, it says:
>
> "If a function or constructor takes two or more arguments, we have
> the option of using it in infix
> form, where we place it between its first and second arguments."
>
> Here is my code:
>
> func2 x y = x + y
>
> func3 x y z  = x + y + z
>
> Here are the results:
>
> *Main> func2 10 20
> 30
> *Main> 10 `func2` 20
> 30
> *Main> func3 10 20 30
> 60
> *Main> 10 `func3` 20 30
>
> <interactive>:1:11:
>    No instance for (Num (t1 -> t))
>      arising from the literal `20' at <interactive>:1:11-15
>    Possible fix: add an instance declaration for (Num (t1 -> t))
>    In the second argument of `func3', namely `20 30'
>    In the expression: 10 `func3` 20 30
>    In the definition of `it': it = 10 `func3` 20 30
>
> How do you get a function to work using infix notation when it has
> 3 arguments?

Prelude> (10 `func3` 20) 30
60

Bob


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

Message: 8
Date: Fri, 20 Mar 2009 20:45:35 +0800
From: ZelluX <zel...@gmail.com>
Subject: [Haskell-beginners] Problems when trying to solve yaht
        exercise 3.10
To: beginners@haskell.org
Message-ID:
        <e487823f0903200545k70d13d20ib85d85a5d9ee6...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi, all

I'm new to haskell and currently reading yaht. I find some problems when
trying to solve exercise 3.10.

The exersices asks to read a list of numbers terminated by a zero, and
figure out the sum and product of the list. My program is as follows:

ex3_10 = do
  hSetBuffering stdin LineBuffering
  numbers <- getNumber
  let sum = foldr (+) 0 numbers
      product = foldr (*) 1 numbers
  putStrLn "The sum is " ++ show(sum)
  putStrLn "The product is " ++ show(product)

getNumber = do
  putStrLn "Give me a number (or 0 to stop):"
  num <- getLine
  if read num == 0
     then return []
     else do
       rest <- getNumber
       return (read num : rest)

But when i load the program, ghci reports error:
    Couldn't match expected type `[a]' against inferred type `IO ()'
    In the first argument of `(++)', namely `putStrLn "The sum is "'
    In a stmt of a 'do' expression:
          putStrLn "The sum is " ++ show (sum)

And i just don't understand the first sentence. Could you tell what does it
mean?

Thanks for your reply
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090320/1ee32429/attachment.htm

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

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


End of Beginners Digest, Vol 9, Issue 23
****************************************

Reply via email to