root = sqrt 5We have the function "sqrt" and it looks for its argument after the white space. This sort of function application is left associative with very high precedence. I think of it as greedy. So "f a b c" is evaluated as ((f a) b) c). It grabs the next thing, so the following may surprise you:
root = sqrt 8 + 1This greedily grabs the 8 and so we get 3.8284. The natural fix is to use parenthesis, but there is a better way.
root = sqrt $ 8 + 1 root = sqrt ( 8 + 1 )Being right associate means "f $ g $ x" is evaluated as f $ ( g $ x )
Because $ is actually a function (and an infix function at that), you can do surprising things with it that we shouldn't get into quite yet.
newfunc = func1 . func2The thing to be careful about is that the type produced by func2 must be the type expected by func1.
putShow = putStrLn . show main = putShow 7This works just fine. Just "PutStrLn 7" would yield an error because it expects its argument to be a string and not an integer.
Tom's software pages / tom@mmto.org