tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
20
Task/Queue-Definition/OCaml/queue-definition-1.ocaml
Normal file
20
Task/Queue-Definition/OCaml/queue-definition-1.ocaml
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
module FIFO : sig
|
||||
type 'a fifo
|
||||
val empty: 'a fifo
|
||||
val push: fifo:'a fifo -> item:'a -> 'a fifo
|
||||
val pop: fifo:'a fifo -> 'a * 'a fifo
|
||||
val is_empty: fifo:'a fifo -> bool
|
||||
end = struct
|
||||
type 'a fifo = 'a list * 'a list
|
||||
let empty = [], []
|
||||
let push ~fifo:(input,output) ~item = (item::input,output)
|
||||
let is_empty ~fifo =
|
||||
match fifo with
|
||||
| [], [] -> true
|
||||
| _ -> false
|
||||
let rec pop ~fifo =
|
||||
match fifo with
|
||||
| input, item :: output -> item, (input,output)
|
||||
| [], [] -> failwith "empty fifo"
|
||||
| input, [] -> pop ([], List.rev input)
|
||||
end
|
||||
28
Task/Queue-Definition/OCaml/queue-definition-2.ocaml
Normal file
28
Task/Queue-Definition/OCaml/queue-definition-2.ocaml
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# open FIFO;;
|
||||
# let q = empty ;;
|
||||
val q : '_a FIFO.fifo = <abstr>
|
||||
# is_empty q ;;
|
||||
- : bool = true
|
||||
# let q = push q 1 ;;
|
||||
val q : int FIFO.fifo = <abstr>
|
||||
# is_empty q ;;
|
||||
- : bool = false
|
||||
|
||||
# let q =
|
||||
List.fold_left push q [2;3;4] ;;
|
||||
val q : int FIFO.fifo = <abstr>
|
||||
|
||||
# let v, q = pop q ;;
|
||||
val v : int = 1
|
||||
val q : int FIFO.fifo = <abstr>
|
||||
# let v, q = pop q ;;
|
||||
val v : int = 2
|
||||
val q : int FIFO.fifo = <abstr>
|
||||
# let v, q = pop q ;;
|
||||
val v : int = 3
|
||||
val q : int FIFO.fifo = <abstr>
|
||||
# let v, q = pop q ;;
|
||||
val v : int = 4
|
||||
val q : int FIFO.fifo = <abstr>
|
||||
# let v, q = pop q ;;
|
||||
Exception: Failure "empty fifo".
|
||||
Loading…
Add table
Add a link
Reference in a new issue