Aniruddh Khera
Aniruddh Khera

Reputation: 121

SML: Interpret function from its type

I am new to SML (meta-language). Can anyone tell me how to derive the function from the type given as below: (’a -> ’b) -> (’b list -> ’c) -> ’a -> ’c list

I am having a hard time in understanding the curried functions in SML.

Upvotes: 0

Views: 106

Answers (1)

Ray Toal
Ray Toal

Reputation: 88378

This will work

- fun f g h x = [h [g x]];
> val ('a, 'b, 'c) f = fn : ('a -> 'b) -> ('b list -> 'c) -> 'a -> 'c list

Here's how I did it.

We are given the type

('a -> 'b) -> ('b list -> 'c) -> 'a -> 'c list

So we know we want a function with three curried arguments, the first two are functions, and the third is anything. So we write:

fun f g h x = ....

Now the first argument is a function that takes in something of type 'a, which x is so we want a

g x

on the right hand side. That will be of type 'b. Now h takes in a 'b list, so we can write

h [g x]

This produces a value of type 'c, but we want f to return a 'c list so we just put that in a list, so we get:

fun f g h x = [h [g x]];

Upvotes: 2

Related Questions