From: Dawid Toton <d0@wp.pl>
To: caml-list@inria.fr
Subject: Induction over types
Date: Thu, 31 Jan 2008 17:38:33 +0000 [thread overview]
Message-ID: <47A20799.4030706@wp.pl> (raw)
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
next reply other threads:[~2008-01-31 17:38 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-01-31 17:38 Dawid Toton [this message]
2008-01-31 18:18 ` [Caml-list] " Christopher L Conway
2008-01-31 21:43 ` Dirk Thierbach
2008-02-01 21:31 ` Christophe TROESTLER
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=47A20799.4030706@wp.pl \
--to=d0@wp.pl \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox