Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2010-12-16 (13:34)
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)

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
      [] ->
	    print_string "\n> ";
      | (text,key,_,_)::rest -> 
	  print_string ("\n"^key^"- "^text);

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

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

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:
Sent from the Caml Discuss mailing list archive at