Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
10
Task/Variadic-function/OCaml/variadic-function-1.ocaml
Normal file
10
Task/Variadic-function/OCaml/variadic-function-1.ocaml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
let rec print = function
|
||||
| [] -> ()
|
||||
| x :: xs -> print_endline x; print xs
|
||||
|
||||
(* Or better yet *)
|
||||
let print = List.iter print_endline
|
||||
|
||||
let () =
|
||||
print [];
|
||||
print ["hello"; "world!"]
|
||||
12
Task/Variadic-function/OCaml/variadic-function-2.ocaml
Normal file
12
Task/Variadic-function/OCaml/variadic-function-2.ocaml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
type 'a variadic =
|
||||
| Z : unit variadic
|
||||
| S : 'a variadic -> (string -> 'a) variadic
|
||||
|
||||
let rec print : type a. a variadic -> a = function
|
||||
| Z -> ()
|
||||
| S v -> fun x -> Format.printf "%s\n" x; print v
|
||||
|
||||
let () =
|
||||
print Z; (* no arguments *)
|
||||
print (S Z) "hello"; (* one argument *)
|
||||
print (S (S (S Z))) "how" "are" "you" (* three arguments *)
|
||||
22
Task/Variadic-function/OCaml/variadic-function-3.ocaml
Normal file
22
Task/Variadic-function/OCaml/variadic-function-3.ocaml
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
type 'a variadic =
|
||||
| Z : unit variadic
|
||||
| U : 'a variadic -> (unit -> 'a) variadic
|
||||
| S : 'a variadic -> (string -> 'a) variadic
|
||||
| I : 'a variadic -> (int -> 'a) variadic
|
||||
| F : 'a variadic -> (float -> 'a) variadic
|
||||
(* Printing of a general type, takes pretty printer as argument *)
|
||||
| G : 'a variadic -> (('t -> unit) -> 't -> 'a) variadic
|
||||
| L : 'a variadic -> (('t -> unit) -> 't list -> 'a) variadic
|
||||
|
||||
let rec print : type a. a variadic -> a = function
|
||||
| Z -> ()
|
||||
| U v -> fun () -> Format.printf "()\n"; print v
|
||||
| S v -> fun x -> Format.printf "%s\n" x; print v
|
||||
| I v -> fun x -> Format.printf "%d\n" x; print v
|
||||
| F v -> fun x -> Format.printf "%f\n" x; print v
|
||||
| G v -> fun pp x -> pp x; print v
|
||||
| L v -> fun pp x -> List.iter pp x; print v
|
||||
|
||||
let () =
|
||||
print (S (I (S Z))) "I am " 5 "Years old";
|
||||
print (S (I (S (L (S Z))))) "I have " 3 " siblings aged " (print (I Z)) [1;3;7]
|
||||
13
Task/Variadic-function/OCaml/variadic-function-4.ocaml
Normal file
13
Task/Variadic-function/OCaml/variadic-function-4.ocaml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
let print f = f ()
|
||||
let arg value () cont = cont (Format.printf "%s\n" value)
|
||||
let stop a = a
|
||||
|
||||
let () =
|
||||
print stop;
|
||||
print (arg "hello") (arg "there") stop;
|
||||
|
||||
(* use a prefix operator for arg *)
|
||||
let (!) = arg
|
||||
|
||||
let () =
|
||||
print !"hello" !"hi" !"its" !"me" stop
|
||||
13
Task/Variadic-function/OCaml/variadic-function-5.ocaml
Normal file
13
Task/Variadic-function/OCaml/variadic-function-5.ocaml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
type ('f,'g) t =
|
||||
| Z : ('f,'f) t
|
||||
| S : 'a -> (('a -> 'f), ('f,'g) t -> 'g) t
|
||||
|
||||
let rec apply: type f g. f -> (f,g) t -> g =
|
||||
fun k t -> match t with
|
||||
| Z -> k (* type g = f *)
|
||||
| S x -> apply (k x) (* type g = (f,g) t -> g *)
|
||||
|
||||
let (!) x = S x (* prefix *)
|
||||
|
||||
(* top level *)
|
||||
# apply List.map !(fun x -> x+1) ![1;2;3] Z
|
||||
Loading…
Add table
Add a link
Reference in a new issue