Ficha OCaml 1 : tipagem, avaliação, errosEx. Tipagem.Ex. Tipagem ou erro.Ex. mistery.Ex. mistério.Ex. Erros.Ex. Tipo e valor.Ex. Qual tipo?Ex. Verdade ou falso?Ex. Qual o tipo?Ex. Tipagem.Ex. Manipulação de tuplosEx. Chamadas à funções.
Dê o tipo das expressões seguintes:
let f x = x * 2
let f x y = x +. (float y)
let x = sin 5. in 2.*. x
let x = 3. in let y = 2 in if x > 1. then 3. ** 9. else cos 1.
let f x (y,z) = if x then y else not z
let f x y z = (x <= y, z)
fun x y z -> (x <= y, if z <= x then x else y)
let f x y z = (x <= y, if z <= x then x else y)
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 f1 x g z = if x then g x else g z
let f2 f g x = if f x then g x
let f3 g x y = if g x then g y
let rec f4 x y = f4 y ([]::x)
let f5 g f x = if x<0 then f (g x) else g (f x)
let f6 g f x y1 = if x<y then f (g x) else g (f y)
let f7 x y = if x > 0 || y < 0. then x else y
Considere a seguinte função mistery
:
1let rec mistery x y z =
2 if y <= 0 then z else mistery x ( y - 1) (x * z)
mistery 2 3 1
?z
é um, explique o que calcula esta função.
Considere a seguinte função misterio
:
xxxxxxxxxx
61let rec misterio d r=
2 if d = 0 then r
3 else
4 let a = (d lsr 1) in
5 let b = (string_of_int (d land 1)) in
6 misterio a (b ^ r)
misterio
.misterio 13 ""
?d
é um inteiro natural e que r
é a string vazia ""
, explique o que calcula esta função.
Avalie cada uma das expressões seguintes, observe as mensagens de erro resultantes e:
Explique cada uma delas.
Proponha uma correção, i.e. uma expressão correta que resolva o problema detetado.
Tenha em atenção que o local assinalado para o erro é o local em que esse é detetado. Nem é sempre o local onde o erro tem origem!
let quarto_pi = 3.14 / 4.
let na_ordem = 1 < 2 < 3
let positivo = let a = 42 in if a >= 0 then true
let duplo_positivo = let x = cos 5. in (if (x < 0) then x else -x) *. 2
Dê o tipo e o valor das seguintes expressões OCaml. No caso de erro, explique a origem e dê uma possível correção.
let r = let x = 7 in 6 * x
let a = (r -6) / 6 - 6
let o = r * r - x * x - 51
`let u = let x = 9 in if (x<9) then 9 / (x-x) else (x+x) / 9
let x = let a = 10 in if a>7 || b / (a - a) then "ola" else "tudo bem"
let x = let a = 10 in if a>7 || b / (a - a) then "ola"
let x = let a = 10 in if a < 7 && b / (a - a) then "ola" else "tudo bem"
Para cada uma das expressões seguintes, indicar, das escolhas possíveis, qual é o tipo certo.
let f (x:int) = x + 1
int -> int
int -> int -> int
int -> unit
unit -> int -> int
let x = 1
int -> int
int
unit -> int
ERROR
let x y = y + 1
int -> int
int -> int -> int
ERROR
let f x y = x +. y
int -> int -> int
unit -> int -> int -> int
float -> float -> float
int -> int -> float
let f1 x y = Printf.printf "%d" x; y + 3
int -> int -> unit -> int
int -> int -> int
unit -> int -> unit -> int
unit -> int -> int
Verdade ou falso, as expressões seguintes estão bem tipificadas.
let s1 = "a" + "b"
Verdade
Falso
let s2 = "a" ^ "b"
Verdade
Falso
let s3 = "a" ^ 'b'
Verdade
Falso
let s4 = 'a' ^ 'b'
Verdade
Falso
Dê o tipo das funções seguintes:
let f x = if x = 2 then Printf.printf "x = 2" else Printf.printf "x <> 2"
int -> int -> unit
int -> int -> unit -> unit
int -> unit
unit
ERROR
let f x y = if x = 2. then x else y+3
int -> int -> int
float -> int -> float -> int
float -> int -> unit
ERROR
let f x = let y = x +. 4. /. 10. in if y > 3.1 then y else 3.
int -> int
float -> unit -> float
float -> float
ERROR
let f = (if 2 > 3 then true else true) || false
bool -> bool
bool
int -> int -> bool
ERROR
Considere a seguinte definição: let a,b = (10, "hello")
Dê o tipo e o valor de a
?
int
e 10
string
e "hello"
Erro: faltam parêntesis
Dê o tipo e o valor de b
?
int
e 10
string
e "hello"
Erro: faltam parêntesis
Considere a seguinte definição: let n1 = (1,2,3,4,5)
Como aceder exclusivamente ao quarto elemento de n1
?
n1[3]
n1[4]
let a,b,c,d,e = n1
let a,b,c,_,e = n1
let _,_,_,a,_ = n1
Seja f
uma função do tipo (string * string) -> string
Expressão seguinte corresponde a uma chamada correta da função f
?
f a b c
f (a,b)
f "hello" "world"
f ("hello","world")