6

I am beginner in Haskell .

The convention used in function definition as per my school material is actually as follows

function_name arguments_separated_by_spaces = code_to_do

ex :

f a b c = a * b +c

As a mathematics student I am habituated to use the functions like as follows

function_name(arguments_separated_by_commas) = code_to_do

ex :

f(a,b,c) = a * b + c

Its working in Haskell .

My doubt is whether it works in all cases ?

I mean can i use traditional mathematical convention in Haskell function definition also ?

If wrong , in which specific cases the convention goes wrong ?

Thanks in advance :)

3 Answers 3

13

Let's say you want to define a function that computes the square of the hypoteneuse of a right-triangle. Either of the following definitions are valid

hyp1 a b = a * a + b * b

hyp2(a,b) = a * a + b * b

However, they are not the same function! You can tell by looking at their types in GHCI

>> :type hyp1
hyp1 :: Num a => a -> a -> a

>> :type hyp2
hyp2 :: Num a => (a, a) -> a

Taking hyp2 first (and ignoring the Num a => part for now) the type tells you that the function takes a pair (a, a) and returns another a (e.g it might take a pair of integers and return another integer, or a pair of real numbers and return another real number). You use it like this

>> hyp2 (3,4)
25

Notice that the parentheses aren't optional here! They ensure that the argument is of the correct type, a pair of as. If you don't include them, you will get an error (which will probably look really confusing to you now, but rest assured that it will make sense when you've learned about type classes).

Now looking at hyp1 one way to read the type a -> a -> a is it takes two things of type a and returns something else of type a. You use it like this

>> hyp1 3 4
25

Now you will get an error if you do include parentheses!

So the first thing to notice is that the way you use the function has to match the way you defined it. If you define the function with parens, you have to use parens every time you call it. If you don't use parens when you define the function, you can't use them when you call it.

So it seems like there's no reason to prefer one over the other - it's just a matter of taste. But actually I think there is a good reason to prefer one over the other, and you should prefer the style without parentheses. There are three good reasons:

  1. It looks cleaner and makes your code easier to read if you don't have parens cluttering up the page.

  2. You will take a performance hit if you use parens everywhere, because you need to construct and deconstruct a pair every time you use the function (although the compiler may optimize this away - I'm not sure).

  3. You want to get the benefits of currying, aka partially applied functions*.

The last point is a little subtle. Recall that I said that one way to understand a function of type a -> a -> a is that it takes two things of type a, and returns another a. But there's another way to read that type, which is a -> (a -> a). That means exactly the same thing, since the -> operator is right-associative in Haskell. The interpretation is that the function takes a single a, and returns a function of type a -> a. This allows you to just provide the first argument to the function, and apply the second argument later, for example

>> let f = hyp1 3
>> f 4
25

This is practically useful in a wide variety of situations. For example, the map functions lets you apply some function to every element of a list -

>> :type map
map :: (a -> b) -> [a] -> [b]

Say you have the function (++ "!") which adds a bang to any String. But you have lists of Strings and you'd like them all to end with a bang. No problem! You just partially apply the map function

>> let bang = map (++ "!")

Now bang is a function of type**

>> :type bang
bang :: [String] -> [String]

and you can use it like this

>> bang ["Ready", "Set", "Go"]
["Ready!", "Set!", "Go!"]

Pretty useful!

I hope I've convinced you that the convention used in your school's educational material has some pretty solid reasons for being used. As someone with a math background myself, I can see the appeal of using the more 'traditional' syntax but I hope that as you advance in your programming journey, you'll be able to see the advantages in changing to something that's initially a bit unfamiliar to you.


* Note for pedants - I know that currying and partial application are not exactly the same thing.

** Actually GHCI will tell you the type is bang :: [[Char]] -> [[Char]] but since String is a synonym for [Char] these mean the same thing.

Sign up to request clarification or add additional context in comments.

5 Comments

On your first notes: 'not exactly the same thing' implies some relation, where there really is none. Currying just allows easy partial application, but otherwise they're rather unrelated in principle, I think.
@Xeo They are somewhat related. The curried/tupled forms form an adjunction where F is (- x B) and G is -^B.
@Xeo I guess it's a point of taste. For a function (a,b,c) -> d, currying it gives a -> b -> c -> d whereas partially applying the first argument gives a a -> (b,c) -> d. For functions of two arguments, currying and partial application are the same thing. In Haskell there's barely a distinction because of currying by default (this was the kind of comment I hoped to avoid making with my footnote!)
@Chris: Partial application doesn't yield a -> (b, c) -> d, but, as the name implies, applies part of the function, yielding (b, c) -> d.
@Xeo Sure, I included the type of the argument you're applying and you didn't. I don't think it really matters. This is why mentioning partial application and currying in the same breath is almost always a bad idea.
2
f(a,b,c) = a * b + c

The key difference to understand is that the above function takes a triple and gives the result. What you are actually doing is pattern matching on a triple. The type of the above function is something like this:

(a, a, a) -> a

If you write functions like this:

f a b c = a * b + c

You get automatic curry in the function. You can write things like this let b = f 3 2 and it will typecheck but the same thing will not work with your initial version. Also, things like currying can help a lot while composing various functions using (.) which again cannot be achieved with the former style unless you are trying to compose triples.

Comments

2
  1. Mathematical notation is not consistent. If all functions were given arguments using (,), you would have to write (+)((*)(a,b),c) to pass a*b and c to function + - of course, a*b is worked out by passing a and b to function *.

  2. It is possible to write everything in tupled form, but it is much harder to define composition. Whereas now you can specify a type a->b to cover for functions of any arity (therefore, you can define composition as a function of type (b->c)->(a->b)->(a->c)), it is much trickier to define functions of arbitrary arity using tuples (now a->b would only mean a function of one argument; you can no longer compose a function of many arguments with a function of many arguments). So, technically possible, but it would need a language feature to make it simple and convenient.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.