Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
1,858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.73 KB | None | 0 0
  1.  
  2. portable:ocamlftvb dan$ ls
  3. README.md _build    lib       lib_test  lwt
  4. portable:ocamlftvb dan$ ls -l lib/
  5. total 24
  6. -rw-r--r--  1 dan  staff   754 29 Mar 17:53 ch3.ml
  7. -rw-r--r--  1 dan  staff  2641  7 Apr 13:58 ch4.ml
  8. -rw-r--r--  1 dan  staff  1984  7 Apr 15:19 ch5.ml
  9. portable:ocamlftvb dan$ corebuild lib/ch5.native
  10. + 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
  11. File "lib/ch5.ml", line 94, characters 17-21:
  12. Error: Unbound value take
  13. Command exited with code 2.
  14. Compilation unsuccessful after building 2 targets (1 cached) in 00:00:00.
  15. portable:ocamlftvb dan$ cat lib/ch
  16. ch3.ml  ch4.ml  ch5.ml
  17. portable:ocamlftvb dan$ cat lib/ch5.ml
  18. (* examples and exercises from chapter 5  *)
  19.  
  20. (* we start with an implementation of the
  21.  * insertionsort algorithmn *)
  22.  
  23. let rec insert (e : int) (l : int list) =
  24.   match l with
  25.   | [] -> [e]
  26.   | hd :: tl ->
  27.       if e <= hd then
  28.         e :: hd :: tl
  29.       else
  30.         hd :: insert e tl
  31. let rec sort l =
  32.   match l with
  33.   | [] -> []
  34.   | hd :: tl -> insert hd (sort tl)
  35.  
  36. (* so the time insertionsort above takes to complete
  37. * can be improved with the mergesort algo below *)
  38.  
  39. (* let's start with the merge part *)
  40.  
  41. let rec merge x y =
  42.   match x, y with
  43.   | [], l -> l
  44.   | l , [] -> l
  45.   | hx :: tx, hy :: ty ->
  46.       if hx < hy then
  47.         hx :: merge tx (hy :: ty)
  48.       else
  49.         hy :: merge (hx :: tx) ty
  50.  
  51. (* now the msort part *)
  52. let rec msort l =
  53.   match l with
  54.   | [] -> []
  55.   | [x] -> [x]
  56.   | _ ->
  57.       let left = take (length l / 2) l in
  58.       let rigth = drop (length l / 2) l in
  59.       merge (msort left) (msort rigth)
  60. portable:ocamlftvb dan$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement