So. I'm actually tinkering with the Idris language, by somewhat following Brady's Type-Driven Development with Idris. I don't think what I written here is that tied to a specific programing language (and I don't know any Haskell, for that matter). But I don't know where else I could post this, given that I have no knowledge of partial-application/currying, types, lambdas and all that stuff from the mathematician's perspective.
Some context
In the second chapter of the book the author draws attention to the following scenario.
Given the self-explaining snippet
double : Num a => a -> a double x = x + x rotate : Shape -> Shape
where
Shape : Type
androtate
are holes for the type of a shape and for a function that rotates aShape
by 90 degrees respectively, there is an obvious pattern behind aquadruple
and aturn-around
functionquadruple : Num a => a -> a quadruple x = double (double x) turn_around : Shape -> Shape turn around x = rotate (rotate x)
which lead us to write a
twice
(high-order) function capable of applying two times the same operator.
To my eyes, there are mostly two ways to solve the problem. The first is just to follow Brady's code
twice : (ty -> ty) -> ty -> ty
twice f x = f (f x)
where he actually defines the image twice f : ty -> ty
of the twice
function over an arbitrary f
1.
The second one, which seems to me a little bit more elegant, is to define twice
by the means of a composite
function and/or an anonymous function, by changing a little bit its signature
twice : (ty -> ty) -> ty
twice f = composite f f
composite : (ty_2 -> ty_3) -> (ty_1 -> ty_2) -> (ty_1 -> ty_3)
composite g f = \x => g (f x)
Both approaches lead to the final result
turn_around : Shape -> Shape
turn_around = twice rotate
The question(s)
I'll try to keep my questions as clear as I can, so instead of mistreating elementary compsci terminology I will keep things concrete.
Let's say we have a "multivariable" function
f : ty_1 -> ty_2 -> ... -> ty_n
Then
f
is a function takingx_1 : ty_1
to another functionf x_1 : ty_1 -> ... -> ty_n
. When should we choose definef
by writingf x_1 = stuff
instead of
f x_1 ... x_{n-2} = stuff2
Could someone clarify me the differences between two two approaches (Brady's and mine) reported above?
1Yes, I'm a math student...