Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* Induction over types
@ 2008-01-31 17:38 Dawid Toton
  2008-01-31 18:18 ` [Caml-list] " Christopher L Conway
  0 siblings, 1 reply; 4+ messages in thread
From: Dawid Toton @ 2008-01-31 17:38 UTC (permalink / raw)
  To: caml-list

I want to write a polymorphic function that invokes itself recursively, 
but with different types.
For example I'd like to translate the following pseudo-code into OCaml:

let rec deep_rev (lst:'a list) = List.rev (List.map (deep_rev:'a->'a) lst)
let deep_rev (element:'a) = element  (* initial induction step *)

let rec super_wrap (depth:int) (lst:'a list) =
  match depth with
   | 0 -> lst
   | d -> super_wrap (d+1) [lst]

And how can I have arbitrary transposition:

val transpose: int list -> 'a list -> 'a list

# transpose [2;0;1]
  [[[1;2];[3;4]]
  ;[[5;6];[7;8]]
  ]      
- : int list list list = [[[1; 3]; [5; 7]]; [[2; 4]; [6; 8]]]

Do I have to have separate functions:

val transpose1: int list -> 'a list -> 'a list   (* identity *)
val transpose2: int list -> 'a list list -> 'a list list
val transpose3: int list -> 'a list list list -> 'a list list list
...

But this is huge amount of redundant code!
Here is for example my transpose3: 
http://www.toton.2-0.pl/OCaml/transpose.ml

Dawid Toton


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-02-01 21:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-31 17:38 Induction over types Dawid Toton
2008-01-31 18:18 ` [Caml-list] " Christopher L Conway
2008-01-31 21:43   ` Dirk Thierbach
2008-02-01 21:31     ` Christophe TROESTLER

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox