History variables: Difference between revisions

Content added Content deleted
Line 986: Line 986:


=={{header|OCaml}}==
=={{header|OCaml}}==
The easiest solution is to use the Stack module coming with OCaml's standard library:
<lang ocaml>
<lang ocaml>
open Stack
module History =
(* This is only for convenience when typing code *)
struct
module H = Stack


let show_entry e =
type hist = { mutable hve: int list }
Printf.printf "History entry: %5d\n" e

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 () =
let hv = H.init() in
let hs = H.create() in
H.push hv 111;
H.push 111 hs ;
H.push hv 4;
H.push 4 hs ;
H.push hv 42;
H.push 42 hs ;
H.show hv;
H.iter show_entry hs;
H.pop hv;
hs |> H.pop |> Printf.printf "%d\n";
H.pop hv;
hs |> H.pop |> Printf.printf "%d\n";
H.pop hv
hs |> H.pop |> Printf.printf "%d\n"
</lang>
</lang>