[Haskell-begin] some basic syntax questions

Alexander Dunlap alexander.dunlap at gmail.com
Fri Jul 25 15:57:59 EDT 2008

On Fri, Jul 25, 2008 at 12:45 PM, Anatoly Vorobey <avorobey at gmail.com> wrote:
> Hello Haskell-beginners,
> I'm trying to work through the "Write Yourself a Scheme in 48 Hours"
> tutorial. One of the
> first exercises calls for writing code to sum up arguments on command line
> and display
> the sum.
> After a few tries, this worked for me:
> module Main where
> import System.Environment
> main :: IO ()
> main = do args <- getArgs
>           putStrLn ("Hello, " ++ show (sumIt args))
>        where sumIt x = sum $ map read x
> However, I have two basic questions:
> 1. I initially tried putStrLn("Hello, " ++ show $ sumIt args), but that
> didn't compile. Why not?
> If I wrap (show $ sumIt args) in parens, it works, but should I have to, if
> I didn't have to in
> the original code above?
> 2. I initially tried
> where sumIt = sum $ map read
> (the "point-free" style, I believe it's called?) but that didn't compile.
> Why not? A friend suggested
> where sumIt = sum . map read
> and that does work; I guess my real problem, then, is that I don't really
> understand the difference
> between the two and why the former doesn't work.
> Thanks in advance,
> Anatoly.
> --
> Anatoly Vorobey, avorobey at gmail.com
> http://avva.livejournal.com (Russian)
> http://www.lovestwell.org (English)
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners


The difference between (.) and ($) is that (.) is composition and ($)
is application. What this means is that (.) takes two /functions/ and
generates a /function/ that is like applying the first function after
the second function:

(f . g) (x) = f (g x)

Where f and g are functions and x is a value. ($), on the other hand,
takes a /function/ and a /value/ and applies the function to the

f $ x = f x

So the reason you can't do sum $ map read is because map read is a
function and sum is a function, so you need to compose the two
functions. If you wanted to use application, you could make a lambda
expression like \x -> sum $ map read x, because now map is fully
applied and is a value for sum.

(Note that I'm oversimplifying a bit here; Haskell doesn't actually
make a distinction between functions and values, but it's easier to
think about like this for simple cases.)


More information about the Beginners mailing list