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:  Simple laplace solver (Edward Z. Yang)
   2.  numerical types, the $ operator (TG)
   3. Re:  numerical types, the $ operator (Brent Yorgey)
   4. Re:  numerical types, the $ operator (John Dorsey)
   5. Re:  numerical types, the $ operator (Brent Yorgey)
   6. Re:  numerical types, the $ operator (John Dorsey)
   7.  question from Yet Another Haskell Tutorial (Michael Mossey)
   8. Re:  question from Yet Another Haskell Tutorial (Daniel Fischer)
   9. Re:  question from Yet Another Haskell Tutorial (Michael Mossey)
  10.  Type inference question (Zachary Turner)


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

Message: 1
Date: Sat, 28 Mar 2009 12:09:00 -0400 (EDT)
From: "Edward Z. Yang" <[email protected]>
Subject: Re: [Haskell-beginners] Simple laplace solver
To: Francesco Bochicchio <[email protected]>
Cc: [email protected]
Message-ID: <alpine.deb.2.00.0903281205390.27...@javelin>
Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed

On Sat, 28 Mar 2009, Francesco Bochicchio wrote:
> There was a thread on this ML which was about an implementation of average 
> which
> avoids
> traversing the list twice, one for 'sum' and one for 'length'. You could use 
> it,
> if you care for
> performance and your list is long enough to matter

The list is four elements long, so I don't particularly care. Although,
I wonder why there isn't a built-in average function.

Cheers,
Edward


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

Message: 2
Date: Sat, 28 Mar 2009 23:18:46 +0200
From: "TG" <[email protected]>
Subject: [Haskell-beginners] numerical types, the $ operator
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Hi all
I'm trying to understand the following simple function

>-- | fractional part of number.
>frac :: (RealFrac a) => a -> a
>frac x = x - fromInteger . floor $ x

which apparently is wrong. Whereas this is ok

>frac x = x - fromInteger (floor x)

Is the 1st one wrong because it is trying to apply the _whole_ 'left of
$' to the 'x' on the right?
How would an experienced guy write this without parentheses?


Moreover, I've put the 'RealFrac' by looking at ":t floor".
What kind of class constraint whould you put for doing eg:

>frac x = x - fromInteger (floor (sqrt x) )

since 'floor' takes fractional and 'sqrt' takes RealFrac? Some kind of
super-class?
Thank you.

PS
Seems that types are half the language, if not more ..


-- 
  TG
  [email protected]

-- 
http://www.fastmail.fm - Access your email from home and the web



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

Message: 3
Date: Sat, 28 Mar 2009 17:34:20 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] numerical types, the $ operator
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Sat, Mar 28, 2009 at 11:18:46PM +0200, TG wrote:
> Hi all
> I'm trying to understand the following simple function
> 
> >-- | fractional part of number.
> >frac :: (RealFrac a) => a -> a
> >frac x = x - fromInteger . floor $ x
> 
> which apparently is wrong. Whereas this is ok
> 
> >frac x = x - fromInteger (floor x)
> 
> Is the 1st one wrong because it is trying to apply the _whole_ 'left of
> $' to the 'x' on the right?

Exactly. ($) has very low precedence, even lower than (-).

> How would an experienced guy write this without parentheses?

I'm fairly certain it's impossible to write it without using
parentheses.  I would probably just write

  x - fromInteger (floor x)


> Moreover, I've put the 'RealFrac' by looking at ":t floor".
> What kind of class constraint whould you put for doing eg:
> 
> >frac x = x - fromInteger (floor (sqrt x) )
> 
> since 'floor' takes fractional and 'sqrt' takes RealFrac? Some kind of
> super-class?

Every instance of RealFrac must also be an instance of Fractional, so
just putting RealFrac would be fine.  (And I didn't have this
memorized, I just started up ghci and typed ':info Fractional' and
':info RealFrac' to see how they are declared.)


> Seems that types are half the language, if not more ..

I think you're on to something there.  One of the great strengths of
Haskell is its strong, expressive static type system.  Type classes
are an especially nifty feature.  Unfortunately, the numeric class
hierarchy leaves a bit to be desired at times, so I hope you won't
draw too many general conclusions from frustrations with numeric
stuff. =)

-Brent


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

Message: 4
Date: Sat, 28 Mar 2009 17:53:54 -0400
From: John Dorsey <[email protected]>
Subject: Re: [Haskell-beginners] numerical types, the $ operator
To: Brent Yorgey <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

> > How would an experienced guy write this without parentheses?
> 
> I'm fairly certain it's impossible to write it without using
> parentheses.  I would probably just write
> 
>   x - fromInteger (floor x)

Never impossible!

flip subtract x . fromInteger $ floor x
case floor x of y -> x - fromInteger y
let y = floor x in x - fromInteger y

I guess I wouldn't choose any of these for readability, though.  Brent's
version is succinct.

Cheers,
John



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

Message: 5
Date: Sat, 28 Mar 2009 18:39:29 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] numerical types, the $ operator
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Sat, Mar 28, 2009 at 05:53:54PM -0400, John Dorsey wrote:
> > > How would an experienced guy write this without parentheses?
> > 
> > I'm fairly certain it's impossible to write it without using
> > parentheses.  I would probably just write
> > 
> >   x - fromInteger (floor x)
> 
> Never impossible!
> 
> flip subtract x . fromInteger $ floor x
> case floor x of y -> x - fromInteger y
> let y = floor x in x - fromInteger y

Hah, you fell right into my trap!  ;)

Well, I actually hadn't thought of these, but I figured someone would find a 
way to do it.

> I guess I wouldn't choose any of these for readability, though.  Brent's
> version is succinct.

Right, the point being, parentheses are not something to be avoided at
all costs.  They should be used as much as necessary to aid
readability, no more, no less.

-Brent


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

Message: 6
Date: Sat, 28 Mar 2009 20:27:11 -0400
From: John Dorsey <[email protected]>
Subject: Re: [Haskell-beginners] numerical types, the $ operator
To: Brent Yorgey <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Brent Yorgey wrote:

> Hah, you fell right into my trap!  ;)

Khan!!

> Right, the point being, parentheses are not something to be avoided at
> all costs.  They should be used as much as necessary to aid
> readability, no more, no less.

Hear, hear.

John



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

Message: 7
Date: Sat, 28 Mar 2009 23:33:13 -0700
From: Michael Mossey <[email protected]>
Subject: [Haskell-beginners] question from Yet Another Haskell
        Tutorial
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

I have a question about a problem in Yet Another Haskell Tutorial 
(problem 7.1). My answers seems to disagree with Hal's, and it fact 
something looks wrong in Hal's answer (maybe it's an error in his 
paper).  The problem is to write the following function in "point-free" 
style:

func5 f list = foldr (\x y -> f(y,x)) 0 list

Here's how I approached it. It's easy to drop 'list' by the 
eta-reduction (using partial application):

func5 f = foldr (\x y -> f(y,x)) 0

But how to get rid of f? First I reasoned that f is a function of a 
tuple. That is, it is not curried. So to curry it:

func5 f = foldr (\x y -> (curry f) y x) 0

The second argument to foldr is obviously flipping the arugments, so

func5 f = foldr (flip $ curry f) 0

Now I want to use the eta-reduction again, but I have to transform this 
expression into something that takes f as its last argument instead of 
the 0. I can use flip again, this time on foldr:

func5 f = flip foldr 0 (flip $ curry f)

Now f can be dropped:

THE FINAL ANSWER: func5 = flip foldr 0 . flip. curry

This works, in a couple of my test cases.  Now Hal gives this as the answer:

func5 = foldr (uncurry $ flip f) 0

The first problem is that there's no argument f, though he refers to it. 
So maybe he meant

func5 f = foldr (uncurry $ flip f) 0

But more problems. He's applying flip to f, but f takes only one 
argument (a 2-tuple). Then he's uncurry-ing it, but I thought it needed 
currying, not uncurry-ing.

Can anyone figure out what Hal is up to, or does it look like a simple 
mistake?

Thanks,
Mike







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

Message: 8
Date: Sun, 29 Mar 2009 14:45:22 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] question from Yet Another Haskell
        Tutorial
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Sonntag 29 März 2009 08:33:13 schrieb Michael Mossey:
> I have a question about a problem in Yet Another Haskell Tutorial
> (problem 7.1). My answers seems to disagree with Hal's, and it fact
> something looks wrong in Hal's answer (maybe it's an error in his
> paper).  The problem is to write the following function in "point-free"
> style:
>
> func5 f list = foldr (\x y -> f(y,x)) 0 list
>
> Here's how I approached it. It's easy to drop 'list' by the
> eta-reduction (using partial application):
>
> func5 f = foldr (\x y -> f(y,x)) 0
>
> But how to get rid of f? First I reasoned that f is a function of a
> tuple. That is, it is not curried. So to curry it:
>
> func5 f = foldr (\x y -> (curry f) y x) 0
>
> The second argument to foldr is obviously flipping the arugments, so
>
> func5 f = foldr (flip $ curry f) 0
>
> Now I want to use the eta-reduction again, but I have to transform 
this
> expression into something that takes f as its last argument instead of
> the 0. I can use flip again, this time on foldr:
>
> func5 f = flip foldr 0 (flip $ curry f)
>
> Now f can be dropped:
>
> THE FINAL ANSWER: func5 = flip foldr 0 . flip. curry
>
> This works, in a couple of my test cases.

As it should, since it is correct.

A simple way to check for sanity of the results is:

Prelude> :t flip foldr 0 . flip. curry
flip foldr 0 . flip. curry :: (Num c) => ((c, b) -> c) -> [b] -> c

Prelude> :t \f list -> foldr (\x y -> f (y,x)) 0 list
\f list -> foldr (\x y -> f (y,x)) 0 list :: (Num b) =>
                                             ((b, a) -> b) -> [a] -> b

Prelude> :t \f -> foldr (uncurry $ flip f) 0
\f -> foldr (uncurry $ flip f) 0 :: (Num b1) =>
                                    (b -> a -> b1 -> b1) -> [(a, b)] -> b1

So you see that your result has the correct type, while Hal's hasn't.

> Now Hal gives this as the
> answer:
>
> func5 = foldr (uncurry $ flip f) 0
>
> The first problem is that there's no argument f, though he refers to 
it.
> So maybe he meant
>
> func5 f = foldr (uncurry $ flip f) 0
>
> But more problems. He's applying flip to f, but f takes only one
> argument (a 2-tuple). Then he's uncurry-ing it, but I thought it 
needed
> currying, not uncurry-ing.
>
> Can anyone figure out what Hal is up to, or does it look like a simple
> mistake?

Simple mistake. YAHT was never systematically proofread to catch all 
such mistakes, so there are several still in. Nevertheless, it is an 
excellent tutorial.

>
> Thanks,
> Mike
>




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

Message: 9
Date: Sun, 29 Mar 2009 06:32:04 -0700
From: Michael Mossey <[email protected]>
Subject: Re: [Haskell-beginners] question from Yet Another Haskell
        Tutorial
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed



Daniel Fischer wrote:
> A simple way to check for sanity of the results is:
> 
> Prelude> :t flip foldr 0 . flip. curry
> flip foldr 0 . flip. curry :: (Num c) => ((c, b) -> c) -> [b] -> c
> 
> Prelude> :t \f list -> foldr (\x y -> f (y,x)) 0 list
> \f list -> foldr (\x y -> f (y,x)) 0 list :: (Num b) =>
>                                              ((b, a) -> b) -> [a] -> b
> 
> Prelude> :t \f -> foldr (uncurry $ flip f) 0
> \f -> foldr (uncurry $ flip f) 0 :: (Num b1) =>
>                                     (b -> a -> b1 -> b1) -> [(a, b)] -> b1
> 
> So you see that your result has the correct type, while Hal's hasn't.
> 
>> Can anyone figure out what Hal is up to, or does it look like a simple
>> mistake?
> 
> Simple mistake. YAHT was never systematically proofread to catch all 
> such mistakes, so there are several still in. Nevertheless, it is an 
> excellent tutorial.

Thanks for the information, glad to know I was correct.

I agree that YAHT is excellent... I am working from several books and 
tutorials simultaneously to get various perspectives, and YAHT covers 
much of the same material as the books, but in a more compact form, with 
good exercises.

-Mike



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

Message: 10
Date: Sun, 29 Mar 2009 11:14:00 -0500
From: Zachary Turner <[email protected]>
Subject: [Haskell-beginners] Type inference question
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Given the following two function definitions:

let func4 l = map (\y -> y+2) (filter (\z -> z `elem` [1..10]) (5:l))
let func4_pf = map (+2) . filter (`elem` [1..10]) . (5 :)

which are equivalent, why does the first one have a type of

func4 :: (Num a, Enum a) => [a] -> [a]

while the second one has a type of

func4_pf :: [Integer] -> [Integer]

Shouldn't the types be the same?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090329/7053aca1/attachment.htm

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

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


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

Reply via email to