Ficha OCaml 4: tipagem, avaliação, erros, Parte 2Ex. Qual o tipo?Ex. Digressões sobre f
.Ex. Terminação e tipagem.Ex. Terminação e tipagem, outra vez.Ex. Verdade ou Falso?Ex. Qual valor?Ex. Tipagem de expressões.Ex. Tipos ou erros.
Para cada uma das expressões seguintes, indicar se são corretamente tipificadas. No caso positivo, dar o referido tipo.
let f1 x = !x
let f2 g x y = if g x then y x else g
let f3 h = let x = ref true in if h x then x:= false; !x
f
.Considere o código seguinte :
1let rec f x =
2 match x with
3 | 0 -> 1
4 | 1 -> 1
5 | n -> f(n-1) + f(n-2)
A função termina sempre?
Verdade
Falso
A função está devidamente tipificada?
int -> int
int -> int -> int
int
ERROR
Considere o código seguinte :
xxxxxxxxxx
51let rec f x y =
2 match x with
3 | 0 -> y
4 | 2 -> Printf.printf "Caso 2\n"; f y 2
5 | n -> Printf.printf "Caso n\n"; f y 0
A função termina sempre?
Verdade
Falso
A função está devidamente tipificada com qual dos tipos ?
int -> int -> int
int -> int -> unit
unit -> unit -> unit
ERROR
Considere o código seguinte :
xxxxxxxxxx
71let rec f x =
2 match x with
3 | 0 -> 0
4 | 1 -> f 0
5 | 2 -> f (x+1)
6 | 3 -> (f 1)+2
7 | n -> n+1
A função termina sempre?
Verdade
Falso
A função está devidamente tipificada com qual dos tipos ?
int
int -> int
ERROR
É possível declarar uma função f
de tipo (int * int * int) -> bool
de pelo menos duas formas diferentes?
Verdade
Falso
As funções seguintes f1
e f2
são equivalentes:
xxxxxxxxxx
131int f1 (int x, int y) { // em C
2 if (x == 1) { return 1;}
3 else if (x == 2) { return 2;}
4 else if (x == y) { return y;}
5 else {return x+2;}
6 }
7
8let f2 x y = (* em OCaml *)
9 match x with
10 | 1 -> 1
11 | 2 -> 2
12 | y -> y
13 | _ -> x+2
Verdade
Falso
Considere a função f
xxxxxxxxxx
71let f x y z =
2match x with
3| 1 -> z - 1
4| 2 -> z + 20
5| y -> y
6| 10 -> z + 5
7| _ -> x
Que valor devolve as expressão f 10 9 0
?
5
9
10
0
Dê o tipo das expressões seguintes:
let x = read_int () in let y = read_float () in (x+1,y)
let f a b c = b (c + a)
let f a b c = b c
let f a (b: int -> int) c = a (b c)
let f a b c = a (b c)
let f a b c = c (b a)
let o = ref (List.tl [1])
let x = ref []
let x = [ ( + ) 3 ; succ ; (fun x y -> List.length x+ y) [1;5] ]
let x y = y 1 in x (fun z -> z + 1)
let f x y = function z -> y (List.map x z)
Para cada uma das alíneas seguintes, dar o tipo da expressão e se não houver tipo por causa de um erro, explique a razão do erro :
let rec ones = 1::ones
Assumindo a declaração anterior, analise List.length ones
fun x -> fun y -> fun z -> x z (y z)
let x = (List.tl ['c']) in 1::x
Assuma a declaração let l = ref []
e a declaração let nl = 1::!l
,
analise esta expressão: let nll = 'c':: !l
(fun x y -> (fun z -> y z) (fun x -> x y) x)
let k = fun x y -> x in let i = fun x -> x in k k i
Considere o seguinte código:
xxxxxxxxxx
61let b = true
2let f0 = fun x -> x+1
3let f = fun x -> if b then f0 else fun y -> x y
4let f = fun x -> if b then f else fun y -> x y
5let f = fun x -> if b then f else fun y -> x y
6let f = fun x -> if b then f else fun y -> x y
Qual é o tipo de do último f
?