Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Continuations
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michaël_Grünewald <michael-grunewald@w...>
Subject: [Caml-list] Re: Continuations
Ceri Storey <cez@mrtall.compsoc.man.ac.uk> writes:

> I was just wondering if there any possibility of there being support
> for continuations in a future version of ocaml?

Are not they possible ? If i remember well (i did not check it back), the
following sample do what is called `Programming with rupture and
continuation' (i learned it from J. Chazarin "Programmer avec Scheme").

Help me: just after the "resoud_probleme" definition, you can spot ";;", the
i can not achieve to remove (i got a syntax error, then).

---- BEGIN CODE ----
type strategie = Abandonne | Ignore | Remplace of int

exception Rupture of (strategie -> int)

let resoud_probleme l =

    let rec continue ax togo strategy =
	match strategy with
	    | Abandonne -> 	ax
	    | Ignore -> 	resoud ax (List.tl togo)
	    | Remplace x -> 	resoud ax (x::(List.tl togo))

    and resoud ax l = 
	match l with 
	    | [] ->	ax
	    | 0::tl -> 	raise (Rupture (continue ax l))
	    | hd::tl -> resoud (ax + hd) tl

    in resoud 0 l
;;

let process_problem l s =
    try  resoud_probleme l
    with Rupture closure -> closure s
    	
in
let strategie_to_string s = 
    match s with
	| Abandonne -> "Abandonne"
	| Ignore -> "Ignore"
	| Remplace x -> Printf.sprintf "Remplace par %d" x

in
let monitor_problem l s =
    Printf.printf "Solution: %d (%s)\n" (process_problem l s) 
    (strategie_to_string s)

in

let main () = 
    let pblm = [10; 23; 33; 0; 12] in
	List.iter (monitor_problem pblm) [Abandonne; Ignore; 
					  Remplace 1; Remplace 12];
	exit 0

in main()
---- END CODE ----

This code is in the awful French/English mix i write when programming, there
is a little lexicon by the end of this message.

Maybe this is not what you call Rupture/Cotninuation (you did not speak of
rupture), but after my memories, this is.

Code explanation: `resoud_problem' must process an horrific and very looong
calculous (the sum of 5 or 6 integers from a list), but take the occurence
of '0' as a critical situation where the calculous cannot be resumed in a
consistent way, it asks the users which Strategy to apply (give up, ignore
critical value, correct the value).

Lexicon :
Abandonne : give up
Ignore : ignore (it could have been 'remove')
Remplace : replace

-- 
Michaël Grünewald <michael-grunewald@wanadoo.fr>  - RSA PGP Key ID: 0x20D90C12
-------------------
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