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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: sieira <jaimito.hendrix@g...>
Subject: [Caml-list] Caml light Callback

I'm trying to implement some kind of callback menu in camllight

what I want is to store every menu entry and action as follows:

type menu == (string*string*triggered_function*args_for_that_function)
list;;

I've been working with C for years, so I may be suffering the consecuences
of my "occupational habits"

Here Is my sketch, where I can't find the way to ask lisp to generalize last
two parameters


(**** given a key, checks if it exists within the menu-entries list ***)
let rec seek = function
    _,[] -> false 
    | x, (name,key,lfun,largs)::l -> if x=key then true
		 else seek (x,l);;

let rec run_op = function 
      _,[] -> ();
      | selection,(name,key,lfun,largs)::l -> if selection = key then lfun
largs else run_op(selection,l);;

let rec draw_menu = function
      [] ->
	  begin
	    print_string "\n> ";
	  end
      | (text,key,_,_)::rest -> 
	begin
	  print_string ("\n"^key^"- "^text);
	  draw_menu(rest);
	end;;

let rec run_menu = function
      menu ->
	begin
	  draw_menu menu;
	  let readen = read_line() in
	  begin
	    if seek(readen,menu) then run_op(readen,menu)
	    else ();
	  end;
	  run_menu menu;
	end;;

let menu_customers = [("Option 1","1",print_string,"Hello world")];;
let menu_operations = [("Option 1","1",run_menu,menu_customers)];;
let menu_principal =
[("Clientes","1",run_menu,menu_customers);("Operaciones","2",run_menu,menu_operations)];;

________________________________
So, while menu_customers has type:
 (string * string * (string -> unit) * string) list
I find that menu_operations has type:
 (string * string * ((string * string * ('a -> unit) * 'a) list -> 'b) *
  (string * string * (string -> unit) * string) list)

Thus, while print_string is (string -> unit), run_menu is ((string * string
* ('a -> unit) * 'a) list -> 'b)
and while "Hello world" is string, menu_customer is  (string * string *
(string -> unit) * string) list.

Is there any way to generalize this two latter parameters, or should I find
another way to do this?

-- 
View this message in context: http://old.nabble.com/Caml-light-Callback-tp30472719p30472719.html
Sent from the Caml Discuss mailing list archive at Nabble.com.