[Haskell-beginners] beginner's type error

Thomas Davie tom.davie at gmail.com
Fri Mar 27 09:11:03 EDT 2009


On 27 Mar 2009, at 14:01, Michael Mossey wrote:

>
>
> Peter Verswyvelen wrote:
>> You can also get rid of the parentheses like this:
>> thing n = n + fromIntegral $ round $ sqrt n
>
> I'm having a hard time finding an explanation of the dollar signs.  
> What do they do? It looks like they break up the left-ro-right  
> association of function names to arguments.

You're pretty much there!

The $ function is simply "apply":
f $ a = f a

The difference is that this version of application (as opposed to the  
version written as ' ') has very very low precidence, and can be used  
to essentially mean "apply this to the whole expression on my right".

Of note though, using chains of ($)s as peter did is commonly  
considered bad style, instead, one should use (.) to build up a  
function, and then apply it, so not:
fromIntegral $ round $ sqrt n
But instead:
fromIntegral . round . sqrt $ n

Why?  Because the latter one has more valid expressions and is  
therefor easier to refactor.  For example, in the latter one I may  
deside that (round . sqrt) is a useful function in itself (lets call  
it integralSqrt) and refactor:

fromIntegral . integralSqrt $ n
integralSqrt = round . sqrt

With this style, this is simply a matter of copy/paste.

> As a beginner, I love how Haskell is filled with so many good ideas,  
> in many areas. The basic concept of functional programming is good,  
> but also Haskell has beautiful syntax that's just pleasing to look  
> at, and also has many convenient features which may not quite  
> qualify as "beautiful" or "elegant" but are just convenient (still a  
> worthy thing).

I'm not sure, most of the convenient things I use in Haskell are also  
beautiful and elegant, did you have something in mind?

Thanks

Bob


More information about the Beginners mailing list