Typage

Quelques questions de typage

Types d'expressions

Donnez les types des expressions suivantes :

  1. ['a', 'b', 'c']
  2. ([False, True], ['0', '1'])
  3. [(False, '0'), (True, '1')]
  4. (['a', 'b'], 'c')
  5. [tail, init, reverse]
  6. take 5

Types de fonctions

Donnez le type des fonctions suivantes :

  1. second xs = head (tail xs)
  2. swap (x, y) = (y, x)
  3. pair x y = (x, y)
  4. fst (x, _) = x
  5. double x = x * 2
  6. palindrome xs = reverse xs == xs
  7. twice f x = f (f x)
  8. j x = (x, [x], [x + 1])
  9. n f x y = f (x + y)

Types plus complexes

Pour chacune des questions suivantes, écrivez le type le plus général correspondant à la fonction décrite.

  1. data Tree a = Leaf a | Node (Tree a) a (Tree a)
    
    tS (Leaf x)     = x
    tS (Node l x r) = tS l + x + tS r
  2. data Tree a = Leaf a | Node (Tree a) a (Tree a)
    
    tS (Leaf x)     = x
    tS (Node l x r) = tS l <= x && x <= tS r
  3. data Expr = Val Float | Add Expr Expr | Mul Expr Expr
    
    eval (Val n) = n
    eval (Add x y) = eval x + eval y
    eval (Mul x y) = eval x * eval y
  4. data Graph a = Graph [(a, [a])]
    
    neighbors x (Graph edges) = concat [ns | (v, ns) <- edges, v == x]
  5. data State = State String
    data Transition = Transition State State
    nextState _ [] = Nothing
    nextState orig@(State s) (Transition (State from) to : ts) 
      | s == from = Just to 
      | otherwise = nextState orig ts