Hoja de ejercicios

Anuncio
Hoja de ejercicios de Inferencia de Tipos.
Programación funcional. Ingeniería Informática. Curso 2011-2012
Inferencia de tipos
Suponiendo conocidos los siguientes tipos,
foldr
zip
concat
map
filter
foldl
flip
(.)
zipWith
::
::
::
::
::
::
::
::
::
∀a, b.(a → b → b) → b → [a] → b
∀a, b.[a] → [b] → [(a, b)]
∀a.[[a]] → [a]
∀a, b.(a → b) → [a] → [b]
∀a.(a → Bool ) → [a] → [a]
∀a, b.(b → a → b) → b → [a] → b
∀a, b.(a → b → c) → (b → a → c)
∀a, b, c.(b → c) → (a → b) → (a → c)
∀a, b, c.(a → b → c) → [a] → [b] → [c]
inferir, siguiendo los pasos habituales, los tipos de las funciones que aparecen en siguientes deniciones
Haskell:
1.
f (u,v) n | u==v
= n+1
| otherwise = n
m
= foldr f
n
= m 0
g t s = zip t s
h x y = n (g x y)
2.
e (u, v) | snd u == fst v = [(fst u, snd v)]
| otherwise
= []
f (x, y) = concat [ e z | z <- zip x y]
3.
g [] _ = []
g (x:y) z | x == z = y
| otherwise = x : g y z
f []
= [[]]
f x
= [v : w | v <- x , w <- f (g x v)]
4.
h x y
g a b | a
| otherwise
f a x
5.
aha
= (foldr1 max) . guay
guay x = map (uhu x) x
uhu x y = length (filter (==y) x)
6.
ex p q = snd x
where x = foldr z (p,-1) y
y = zip q [0..]
z (u,v) (r,s) | u == r
= (u,v)
| otherwise = (r,s)
7.
f
= foldr (&&)
g p = f True . map p
h
= g . (/=)
=
=
=
=
(x==y)
True
b
foldr g False (map (h x) a)
1
8.
g x = concat . map x
h
= g (\ y -> y ++ "\n")
9.
z u v w = if u w then w else z u v (v w)
u
= foldr v []
v w z | null z
= w
| otherwise = w ++ (' ':z)
10.
z x []
= x : []
z x (y:ys) | x <= y
= x : y : ys
| otherwise = y : z x ys
w = foldr z []
11.
z = foldl (flip (:)) []
s = foldr (+) 0
w = s . zipWith (*) [3..] . z
12.
s t z []
= [z]
s t z (x:xs) = z : s t (t z x) xs
13.
f x = let g y = 1 + (x y) in (g 0 , g True)
14.
h f x = \ g -> g (f x g)
15.
h f x = \ g -> g f (f x g)
16.
f g [x] = x
f g (x:xs) = g x (f g xs)
2
Descargar