Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- portable:ocamlftvb dan$ ls
- README.md _build lib lib_test lwt
- portable:ocamlftvb dan$ ls -l lib/
- total 24
- -rw-r--r-- 1 dan staff 754 29 Mar 17:53 ch3.ml
- -rw-r--r-- 1 dan staff 2641 7 Apr 13:58 ch4.ml
- -rw-r--r-- 1 dan staff 1984 7 Apr 15:19 ch5.ml
- portable:ocamlftvb dan$ corebuild lib/ch5.native
- + ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g -bin-annot -short-paths -thread -package core -ppx 'ppx-jane -as-ppx' -I lib -o lib/ch5.cmo lib/ch5.ml
- File "lib/ch5.ml", line 94, characters 17-21:
- Error: Unbound value take
- Command exited with code 2.
- Compilation unsuccessful after building 2 targets (1 cached) in 00:00:00.
- portable:ocamlftvb dan$ cat lib/ch
- ch3.ml ch4.ml ch5.ml
- portable:ocamlftvb dan$ cat lib/ch5.ml
- (* examples and exercises from chapter 5 *)
- (* we start with an implementation of the
- * insertionsort algorithmn *)
- let rec insert (e : int) (l : int list) =
- match l with
- | [] -> [e]
- | hd :: tl ->
- if e <= hd then
- e :: hd :: tl
- else
- hd :: insert e tl
- let rec sort l =
- match l with
- | [] -> []
- | hd :: tl -> insert hd (sort tl)
- (* so the time insertionsort above takes to complete
- * can be improved with the mergesort algo below *)
- (* let's start with the merge part *)
- let rec merge x y =
- match x, y with
- | [], l -> l
- | l , [] -> l
- | hx :: tx, hy :: ty ->
- if hx < hy then
- hx :: merge tx (hy :: ty)
- else
- hy :: merge (hx :: tx) ty
- (* now the msort part *)
- let rec msort l =
- match l with
- | [] -> []
- | [x] -> [x]
- | _ ->
- let left = take (length l / 2) l in
- let rigth = drop (length l / 2) l in
- merge (msort left) (msort rigth)
- portable:ocamlftvb dan$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement