tags:

views:

61

answers:

2

How can I transform several values a,b,c etc. to a',b',c' etc, such that x'=f(x)? The values are bound to specific names and their quantity is known at compile-time.

I tried to apply a function to a list in the following way:

let [a';b'] = List.map f [a;b] in ...

But it yields warning:

Warning P: this pattern-matching is not exhaustive.                                                                                                         
Here is an example of a value that is not matched:                                                                                                          
[]

Any way to avoid it?

+3  A: 

Unfortunately not. You can silence the compiler by writing

match List.map f [a;b] with
  [a';b'] -> ...
| _ -> assert false

but that's all.

Pascal Cuoq
+3  A: 

You can write a few functions for mapping on to uniform tuples, i.e.:

let map4 f (x,y,z,w) = (f x, f y, f z, f w)
let map3 f (x,y,z) = (f x, f y, f z)
let map2 f (x,y) = (f x, f y)

and then you can just use them whenever the need arises.

let (x',y') = map2 f (x,y)
zrr