History variables: Difference between revisions

Line 986:
 
=={{header|OCaml}}==
The easiest solution is to use the Stack module coming with OCaml's standard library:
<lang ocaml>
open HistoryStack
module History =
(* This is only for convenience when typing code *)
struct
module HistoryH = Stack
 
let show_entry e =
type hist = { mutable hve: int list }
List.iter (fun v -> Printf.printf "History entry: %5d\n" v) hv.hvee
 
let init () = { hve = [] }
 
let push h e =
h.hve <- e::h.hve
 
let pop h =
match h.hve with
| [] -> ()
| v::t -> h.hve <- t; print_int v; print_newline ()
 
let show hv =
List.iter (fun v -> Printf.printf "History entry: %5d\n" v) hv.hve
end
 
open History
module H = History
 
let () =
let hv hs = H.initcreate() in
H.push hv 111 hs ;
H.push hv 4 hs ;
H.push hv 42 hs ;
H.showiter hvshow_entry hs;
hs |> H.pop hv|> Printf.printf "%d\n";
hs |> H.pop hv|> Printf.printf "%d\n";
hs |> H.pop hv|> Printf.printf "%d\n"
</lang>
 
Anonymous user