Types of ML for function, litle tips?

I am preparing for the GATE exam. One of the oldest questions is unfamiliar with us. Some experts, please clarify this for us.

Which of the following might be the type for the next ML function?

my f(g, nil)=nil | f(g,x::xs)=(fn a  β‡’ g(a,x))::f(g, xs);

1) (int *book β†’ real) * bool list β†’ (int β†’ real) list
2) (bool *int β†’ int) * real list β†’ (bool β†’ int) list
3) (int *int β†’ real) * real list β†’ (real β†’ bool) list
4) (bool *real β†’ int) * int list β†’ (int β†’ int) list

Answer sheets say (1) That's right. comments or a brief description for a better understanding?

+4
source share
1 answer

One of the first things you should do is rewrite the function definition yourself. This will make you actually make out and understand.

fun f (g, nil)     = nil
  | f (g, x :: xs) = (fn a => g (a, x)) :: f (g, xs);

So, fis a function, even the question says that it should be of type ... -> ...:

val f : ... -> ...

What does he get? Let's look at the first function template:

fun f (g, nil)     = nil

- (a, b), 2-. :

val f : (... * ...) -> ...

, , g , nil , nil - . , -:

val f : (... * ... list) -> ...

? nil, , -, , .

val f : (... * ... list) -> ... list

, :

| f (g, x :: xs) = (fn a => g (a, x)) :: f (g, xs);

, , , ::.

:

(fn a => g (a, x)) :: f (g, xs)

, , , , , .. ... list. .

, , f, . , :

val f : (... * ... list) -> (... -> ...) list

? g 2-. 2- f. , 2-:

val f : (((... * ...) -> ...) * ... list) -> (... -> ...) list

- a, , ? , , g. x? , , g. , - a x? . , g :

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

'a, 'b 'c , .. . . f:

val f : ((('a * 'b) -> 'c) * ... list) -> (... -> ...) list

x, 2-, f, :

val f : ((('a * 'b) -> 'c) * 'b list) -> (... -> ...) list

, .

val f : ((('a * 'b) -> 'c) * 'b list) -> ('a -> 'c) list

, , - :

val f : ('a * 'b -> 'c) * 'b list -> ('a -> 'c) list

. , , - , . ? , . :

1

val f : ('a  * 'b   -> 'c)   * 'b list   -> ('a  -> 'c) list
val f : (int * bool -> real) * bool list -> (int -> real) list

, 'a int, 'b bool (it book , , , ) 'c . , , , , , . :

2

val f : ('a   * 'b  -> 'c)  * 'b list   -> ('a   -> 'c) list
val f : (bool * int -> int) * real list -> (bool -> int) list

-type -type :

  • 'a β†’ bool
  • 'b β†’ int
  • 'c β†’ int
  • 'b β†’ real

, , 'b , , .

3 4

2, :)

+5

Source: https://habr.com/ru/post/1628467/


All Articles