Minimax
Minimax

Reputation: 131

How does Haskell evaluate this signature?

ggt_euklid :: Nat1 -> (Nat1 -> Nat1)

I am trying to learn partial application, I know that in this case, if the parentheses would be left out, I would get the same result, but I do not know how this signature should be evaluated.

As far as I have understood, parentheses signify that it is a function? Would that not imply that ggt_euklid takes a value Nat1 and returns a function?

Below is the complete function:

ggt_euklid x y
| x == y = x
|x>y =ggt_euklid(x-y) y 
|x<y =ggt_euklid x (y-x)

Upvotes: 0

Views: 78

Answers (2)

assembly.jc
assembly.jc

Reputation: 2076

Would that not imply that ggt_euklid takes a value Nat1 and returns a function?

No, it still imply that ggt_euklid takes one argument of type Nat1 and return a function of type Nat1->Nat1, even though, the parentheses be left out.

The Arrow -> always be right-associativity (when no parentheses), i.e.:

Nat1 -> Nat1 -> Nat1 

is equivalent to

Nat1 -> (Nat1 -> Nat1)

which is corresponding to the function application always be left-associativity. (when no parentheses) for example:

ggt_euklid 1 2

is equivalent to

(ggt_euklid 1) 2

Here

(ggt_euklid 1) ~ Nat1 -> Nat1

and

(ggt_euklid 1) 2 ~ Nat1

So, no matter whether one or two arguments apply to ggt_euklid, it always return a function of type Nat1 -> Nat1 firstly, if second argument is provided, it applies second argument to the returned function.

Upvotes: 2

amalloy
amalloy

Reputation: 92117

You have understood the type signature correctly: it takes one argument and returns a function. That's how "multi-argument" functions in Haskell work: through currying. You can see this in action by trying this equivalent implementation:

ggt_euklid :: Nat1 -> (Nat1 -> Nat1)
ggt_euklid x = \y -> result
  where result | x == y = x
               | x > y = ggt_euklid (x-y) y 
               | x < y = ggt_euklid x (y-x)

Here I've introduced this fairly pointless result variable as a thing to use your pattern guards on, but the idea is the same.

Upvotes: 4

Related Questions