= vs ->

Ketil Malde ketil@ii.uib.no
10 Oct 2001 08:28:35 +0200


Mark Carroll <mark@chaos.x-philes.com> writes:

> On Tue, 9 Oct 2001, Ashley Yakeley wrote:

>> At 2001-10-09 11:55, Mark Carroll wrote:

>>> What is the rationale for when Haskell demands a "=" and when it
>>> demands a "->"?

Okay, I can't give you anything formal, but here's my intuitive
understanding of things

> e.g. 

> x :: Integer -> Integer

A function "from" and Integer to an Integer.  Even more obvious if you
have one more parameter:

  g :: Integer -> Integer -> Integer

g takes an Integer and returns a function that takes an Integer and
returns an Integer.  Equals-assignment would be very non-intuitive
here. 

I guess the same argument goes for lambdas

        \x -> x*x

maps *from* an x *to* its square.

> x 1 = 1
> x 2 = 3

Function definitions use (=).  I'm not sure I see any really
compelling reason, except that it's the usual math syntax, and arrows
would look weird, in particular with nullary definitions:

        c -> 0

> y a =
>   case a of
>   1 -> 1
>   2 -> 3

> z a
>  | a == 1 = 1
>  | a == 2 = 3

It seems there's a predisposition for having exactly one (=) in
function definitions.  Perhaps one could have had a syntax like

        z a =
          | a == 1 -> 1
          | a == 2 -> 3

instead, as it'd make it more consisten with the case, but I suppose
there's a reason for it being the way it is.  The case statement is an
expression like any other, while I suspect the guards can only be used
in function definitions like your 'z' example.

By the way, if you read '=' as "is assigned to", and '|' as "where"
and '->' as "gives", things mostly make sense, I think.

(Note that there's also the back-arrow, used to "draw from", e.g. in
the IO Monad

        main = do
                x <- readFile "/etc/passwd"
                putStr (map crack (lines x))

or list comprehensions

        primes = [ p | p <- [2..], noDivides p primes]

I suppose the difference from (=) assignment is reasonably clear.)

Rambling on,

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants