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:  Re: Printing the result of a function        evaluation
      (Brandon S. Allbery KF8NH)
   2.  Better way to write this? (Tim Perry)
   3. Re:  Better way to write this? (Rahul Kapoor)
   4. Re:  Better way to write this? (Mihai Maruseac)
   5. Re:  Better way to write this? (Brent Yorgey)
   6. Re:  Better way to write this? (Tim Perry)
   7. Re:  Library source (Dave Bayer)
   8. Re:  Library source (Daniel Fischer)


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

Message: 1
Date: Tue, 9 Mar 2010 00:01:29 -0500
From: "Brandon S. Allbery KF8NH" <[email protected]>
Subject: Re: [Haskell-beginners] Re: Printing the result of a function
        evaluation
To: Travis Erdman <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Skipped content of type multipart/alternative-------------- next part 
--------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20100308/2a8aa96e/PGP-0001.bin

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

Message: 2
Date: Tue, 9 Mar 2010 12:26:29 -0800 (PST)
From: Tim Perry <[email protected]>
Subject: [Haskell-beginners] Better way to write this?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

I'm working my way through the 99 sample programs for haskell and I'm on #4: 
find the number of elements in a list.
http://haskell.org/haskellwiki/99_questions/1_to_10

I wrote the obvious recursion. Then I rewrote it using foldl
myLength :: [a] -> Int
myLength [] = 0
myLength xs = foldl addOne 0 xs
   where addOne lhs rhs = lhs + 1

However, the solution given uses notation that confuses my little mind. Can 
someone point me to a good resource on this notation?

myLength :: [a] -> Int
myLength =  foldr (\x n -> n + 1) 0


Thanks,
Tim


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

Message: 3
Date: Tue, 9 Mar 2010 15:33:11 -0500
From: Rahul Kapoor <[email protected]>
Subject: Re: [Haskell-beginners] Better way to write this?
To: Tim Perry <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

>However, the solution given uses notation that confuses my
>little mind. Can someone point me to a good resource on this
>notation?
> myLength :: [a] -> Int myLength = foldr (\x n -> n + 1) 0


"\x n -> n + 1" in the above is an anonymous function.

More at:

http://www.haskell.org/haskellwiki/Anonymous_function
http://www.haskell.org/haskellwiki/Lambda_abstraction

Regards,

Rahul


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

Message: 4
Date: Tue, 9 Mar 2010 22:35:06 +0200
From: Mihai Maruseac <[email protected]>
Subject: Re: [Haskell-beginners] Better way to write this?
To: Tim Perry <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On Tue, Mar 9, 2010 at 10:26 PM, Tim Perry <[email protected]> wrote:
> I'm working my way through the 99 sample programs for haskell and I'm on #4: 
> find the number of elements in a list.
> http://haskell.org/haskellwiki/99_questions/1_to_10
>
> I wrote the obvious recursion. Then I rewrote it using foldl
> myLength :: [a] -> Int
> myLength [] = 0
> myLength xs = foldl addOne 0 xs
>   where addOne lhs rhs = lhs + 1
>
> However, the solution given uses notation that confuses my little mind. Can 
> someone point me to a good resource on this notation?
>
> myLength :: [a] -> Int
> myLength =  foldr (\x n -> n + 1) 0
>
>

Your example and that example are the same code, only rewritten.

Starting with your code, we change the arguments of addOne to be x and
n and ignore the case of an empty list since foldl on an empty list
returns the start value.

myLength xs = foldl addOne 0 xs
  where addOne x n = n + 1

Now, instead of using a named function, we inline addOne as a lambda expression:

myLength xs = foldl (\x n -> n + 1) 0 xs

(read the paranthesis as a function with to arguments, x and n, which
computes n + 1).

Now, there's is a style called point free style, in which we don't
give function arguments if they are implied and the code looks
prettier without them. If we do this to our myLength we will arrive
to:

myLength foldl (\x n -> n + 1) 0

which is exactly their solution. Observe that point-free notation is
useful when you have only one occurence of the argument and it is at
the end of the expression.

As for some resources, try looking here[0] or here[1]

[0]: http://learnyouahaskell.com/higher-order-functions#lambdas
[1]: http://book.realworldhaskell.org/read/functional-programming.html
(long reading)

-- 
Mihai Maruseac


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

Message: 5
Date: Tue, 9 Mar 2010 17:28:51 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Better way to write this?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Tue, Mar 09, 2010 at 10:35:06PM +0200, Mihai Maruseac wrote:
> 
> myLength xs = foldl addOne 0 xs
>   where addOne x n = n + 1

Almost, Tim's original addOne is actually 

  addOne x n = x + 1

The difference is that the given solution uses foldr whereas Tim was
using foldl.  Either one works in this case since + is commutative.

-Brent


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

Message: 6
Date: Tue, 9 Mar 2010 15:39:51 -0800 (PST)
From: Tim Perry <[email protected]>
Subject: Re: [Haskell-beginners] Better way to write this?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Thanks Brent, Stephen, Mihai, and Rahul,

I finally understand lambda expressions :)  The combination of your comments 
got me there and "Learn you a haskell" solidified it. I've now written 3 
working lambda expression in my life (without copying).

Thanks again,
--Tim





----- Original Message ----
From: Brent Yorgey <[email protected]>
To: [email protected]
Sent: Tue, March 9, 2010 2:28:51 PM
Subject: Re: [Haskell-beginners] Better way to write this?

On Tue, Mar 09, 2010 at 10:35:06PM +0200, Mihai Maruseac wrote:
> 
> myLength xs = foldl addOne 0 xs
>   where addOne x n = n + 1

Almost, Tim's original addOne is actually 

  addOne x n = x + 1

The difference is that the given solution uses foldr whereas Tim was
using foldl.  Either one works in this case since + is commutative.

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



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

Message: 7
Date: Wed, 10 Mar 2010 09:07:07 -0500
From: Dave Bayer <[email protected]>
Subject: Re: [Haskell-beginners] Library source
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Mar 8, 2010, at 1:05 PM, Stephen Tetley wrote:

> Haven't the 'standard' libs (aka the Hierarchical libs)  they always
> been here with a GHC install?
> 
> <ghc-version>/doc/html/...
> 
> Html docs for GHC, Haskell Hierarchical Libraries (with marked-up) source, 
> Cabal

First, in GHC 6.12 there are fewer standard libraries: Only those needed to 
build GHC itself. Before 6.12 one could learn Haskell and get many things done 
without venturing into Cabal libraries. Now many of the libraries one took as 
"standard" in the past are Cabal installs. Meanwhile, as one gains experience 
with Haskell one ventures beyond the "standard" libraries, and also expects 
full documentation.

Here's a ticket (not started by me) indicating that Cabal's default is not to 
make all possible documentation, and this default is hard to change:

        http://hackage.haskell.org/trac/hackage/ticket/517

I found this ticket because I was missing documentation that I expected. Can't 
tell you which files now; after my work-around I have everything that I expect.

Don't get me wrong, building from source then using "cabal install" is by far 
the best option I know. I just wish that all options (every platform-specific 
binary install, Haskell Platform, source then cabal install) would provide 
every conceivable Haddock HTML file with links.

In calculus we teach students to look both at critical points and the endpoints 
of an interval for optima. I'm saying here that the optimum is the "document 
all source code" end of the interval. It's an easy principle to articulate, and 
we don't follow it. I'm asking why not, and speculating that it's simply a 
dated habit that hasn't been reexamined recently. Hey, my primary drives are 
all solid state, and I have enough room for source code. In the era of $80 TB 
drives, this seems a no-brainer.



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

Message: 8
Date: Wed, 10 Mar 2010 16:03:29 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Library source
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Mittwoch 10 März 2010 15:07:07 schrieb Dave Bayer:
> On Mar 8, 2010, at 1:05 PM, Stephen Tetley wrote:
> > Haven't the 'standard' libs (aka the Hierarchical libs)  they always
> > been here with a GHC install?
> >
> > <ghc-version>/doc/html/...
> >
> > Html docs for GHC, Haskell Hierarchical Libraries (with marked-up)
> > source, Cabal
>
> First, in GHC 6.12 there are fewer standard libraries: Only those needed
> to build GHC itself. Before 6.12 one could learn Haskell and get many
> things done without venturing into Cabal libraries. Now many of the
> libraries one took as "standard" in the past are Cabal installs.

Actually, since 6.8 (or was it even 6.6?), you needed the extralibs bundle 
for a somewhat comprehensive installation to get things going smoothly.
That extralibs bundle has been replaced by/expanded to the platform, which 
is basically a good thing (I hope that, now the base-split is done, the 
platform releases can follow the GHC releases more closely).

> Meanwhile, as one gains experience with Haskell one ventures beyond the
> "standard" libraries, and also expects full documentation.
>
> Here's a ticket (not started by me) indicating that Cabal's default is
> not to make all possible documentation, and this default is hard to
> change:
>
>       http://hackage.haskell.org/trac/hackage/ticket/517
>
> I found this ticket because I was missing documentation that I expected.
> Can't tell you which files now; after my work-around I have everything
> that I expect.
>
> Don't get me wrong, building from source then using "cabal install" is
> by far the best option I know. I just wish that all options (every
> platform-specific binary install, Haskell Platform, source then cabal
> install) would provide every conceivable Haddock HTML file with links.

Aye.

I've spent several hours trying to get into the cabal-install code to offer 
a fix, but I've barely scratched the surface :(
So for the time being, I have to resort to my evil work-around and change 
the Cabal sources for each new release.

>
> In calculus we teach students to look both at critical points and the
> endpoints of an interval for optima. I'm saying here that the optimum is
> the "document all source code" end of the interval.

+1

> It's an easy
> principle to articulate, and we don't follow it. I'm asking why not, and
> speculating that it's simply a dated habit that hasn't been reexamined
> recently. Hey, my primary drives are all solid state, and I have enough
> room for source code. In the era of $80 TB drives, this seems a
> no-brainer.

I have only 150GB and I've plenty of space for source code and hscoloured 
sources too.


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

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


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

Reply via email to