English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Dynamically evaluating OCaml code
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-04-08 (17:58)
From: Jean-Marc EBER <jeanmarc.eber@l...>
Subject: Re: [Caml-list] Dynamically evaluating OCaml code

John Goerzen wrote:

> Now, I know that I must ignore the first two and last three elements in
> that list.  I do not know in advance what size the list will be, and it
> varies from line to line, but it always has at least five elements.  I
> also do not necessarily know the values for "2" and "3" at compile time,
> though they are constant throughout execution of the program.  The order
> of the elements is significant and must not be altered.  Each element is
> the same type (ie, a String).  There is nothing in the data itself that
> differentiates it.
> Now, which OCaml data structure gives me the ability to easily pull out
> such a slice?  As far as I can tell, there are no standard functions for
> any of Array or List to do that.  I could write a function for either of
> them to loop over it and get me what I want, but the point is that this
> functionality is useful.

In the time you wrote the previous paragraph, its also possible to write 
following (of course horribly inefficient, because it inverts two times the 
argument list) prototype implementation of the function you need, so that you 
can continue to work on your problem. When you hit efficiency problems, you will 
probably later on replace it with a better one...

let remove_2_3 = function
   | _ :: _ :: rest ->
       (match List.rev rest with
       | _ :: _ :: _ :: rest -> List.rev rest
       | _ -> failwith "error2")
   | _ -> failwith "error1";;

remove_2_3 [1; 2; 3; 4; 5; 6; 7];;

cd c:/cygwin/home/jmeber/mlfi/
ocaml eber.ml
[3; 4]

Compilation finished at Thu Apr 08 19:45:26

I don't need to mention that your previous particular problem is so trivial that 
it could easily be expressed with arrays as a "one liner" of caml code... Look 
at Array.sub...


To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners