a | b c | d
- https://simon.cedeela.fr
- @[email protected]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module G = struct | |
| type 'a t = unit -> 'a option | |
| let (--) i j = | |
| let r = ref i in | |
| fun () -> | |
| if !r > j then None | |
| else (let x = !r in incr r; Some x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* ocamlfind opt -g -package zarith -package containers.iter -linkpkg primes.ml -o primes *) | |
| module L = CCLazy_list;; | |
| let gen_int = | |
| let n = ref Z.one in | |
| fun () -> | |
| let x = !n in | |
| n := Z.succ !n; | |
| Some x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #require "sequence";; | |
| open Sequence.Infix;; | |
| let rec perms = function | |
| | [] -> Sequence.return [] | |
| | x :: tail -> perms tail >>= insert x | |
| and insert x l = match l with | |
| | [] -> Sequence.return [x] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #require "sequence";; | |
| open Sequence.Infix;; | |
| (* all the combinations *) | |
| let rec combs = function | |
| | [] -> Sequence.return [] | |
| | x :: tail -> | |
| Sequence.append (combs tail) (combs tail >|= fun l -> x :: l);; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* ocamlfind opt -package sequence,benchmark -linkpkg truc.ml -o truc; ./truc*) | |
| open Sequence.Infix | |
| (* all the combinations *) | |
| let rec combs = function | |
| | [] -> Sequence.return [] | |
| | x :: tail -> | |
| Sequence.append (combs tail) (combs tail >|= fun l -> x :: l) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* ocamlfind opt -package sequence,benchmark -linkpkg truc.ml -o truc; ./truc*) | |
| open Sequence.Infix | |
| type op = | |
| | Add | |
| | Mult | |
| | Div | |
| | Minus |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| open Format | |
| type tp = Ti | To | Arr of tp * tp | |
| let pp_wrap condition pp_fmt fmt x= | |
| if condition then | |
| fprintf fmt "(%a)" pp_fmt x | |
| else | |
| fprintf fmt "%a" pp_fmt x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #require "bigstring";; | |
| #require "sequence";; | |
| (* the goal is to count how many times each character occurs in the file *) | |
| let file = "foo.txt";; | |
| (* simple way *) | |
| Bigstring.with_map_file file | |
| (fun map -> |
One of the most common complaint about OCaml, from both newcomers and veterans, is that the stdlib is lacking in several domains. Among these we can list:
- some modules are absent but should exist (e.g.
Option) - some modules are present but lack some functionality
(e.g.
Listcould have many more combinators) - the lack of some transverse features (iterators, printers, monadic operators…)
- the lack of generality of some constructs. Notably,
in_channel