Browse thread
Snd question
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | skaller <skaller@u...> |
| Subject: | Re: [Caml-list] Snd question |
On Mon, 2005-08-15 at 16:41 -0600, Matt Gushee wrote:
> Anu Engineer wrote:
>
> > Please forgive me if my question is very naïve, I am very new to
> Ocaml. I was
>
> > wondering why snd returns an error when I use it with more than 2
> elements, why
>
> > not return the rest of the list when I apply to something larger than
> a pair ?
>
>
> Your question shows your misunderstanding: snd operates on tuples, not
> lists.
I think the original question really meant:
Why aren't "fst" and "snd" properly generic??
For example this simply *should* work:
snd (1,2,3)
and even pattern matching can't handle it:
match e with (h,t,...) ->
also should work. Felix actually supports this ugly generic
tuple projection operator:
(1,2.1).(1) // generic n'th of tuple
(1,2.1,3).(1) // n must be compile time constant
and even:
var i:int;
for_each {i=0;} {i<3} {++i;} {
print (1,2,3).[i]; // run time indexing
endl;
};
because a tuple of all the same type is an array,
but as yet I haven't found a way to make ... work in
pattern matches.
It would actually be nice to have more general support
for polyadic tuple management: for example thinking about
obtaining a slice of a tuple, or concatenating two tuples,
suggests that just getting the n'th component is special case.
This kind of thing can actually be done in a hacky way
within limits with C++ template meta-programming.
A more general mapping from a list of types (at compile time)
representing tuples should be possible: any list operation
on types is a run-time operation on values. Unfortunately
I doubt camlp4 can handle that, since it is dealing with untyped
terms. Perhaps MetaOcaml can do it?
I would love to do this in Felix .. but it requires dynamic loading.
--
John Skaller <skaller at users dot sourceforge dot net>