(* hogeは int lazy_t 型 *)let hoge = lazy 1
$ cat test_ocaml.mllet hoge = lazy 1 + 2$ omake*** omake: reading OMakefiles*** omake: finished reading OMakefiles (0.01 s...
(* hogeは int lazy_t 型 *)let hoge = lazy (1 + 2)let rec map f s = lazy (match s with  | (lazy SSnil) -> SSnil  | (lazy (SSc...
$ cat test_ocaml.mllet hoge = lazy (1 + 2)let _ = print_int (Lazy.force hoge)(* (Lazy.force hoge) は Int 型 *)$ ./test_ocaml3
$ cat test_ocaml.mllet hoge = lazy (1 + 2)let _ = match hoge with  | lazy i -> print_int i$ ./test_ocaml3
let plus (lazy m) (lazy n) = lazy (m + n)let _ = let i = Lazy.force (plus (lazy 1) (lazy 2)) in         print_int i
fun lazy plus ($m, $n) = $m+nfun plus (x, y) = $case (x, y) of ($m, $n) => force ($m+n)(* plus : int lazy_t -> int lazy_t ...
datatype a StreamCell = NIL | CONS of a * a Streamwithtype a Stream = a StreamCell susptype a cell = SSnil | SScons of a *...
fun lazy ($NIL) ++ t = t     | ($CONS (x, s)) ++ t = $CONS (x, s ++ t)let rec (++) t1 t2 = lazy (match (t1, t2) with  | (l...
fun lazy take (0, s) = $NIL     | take (n, $NIL) = $NIL     | take (n, $CONS (x, s)) = $CONS (x, take (n - 1, s))let rec t...
fun lazy drop (n, s) = let fun drop (0, s) = s      | drop (n, $NIL) = $NIL      | drop (n, $CONS (x, s)) = drop (n - 1, s...
fun lazy reverse s = let fun reverse ($NIL, r) = r      | reverse ($CONS (x, s), r) = reverse (s, $CONS (x, r)) in reverse...
https://github.com/master-q/readPurelyFunctionalDataStructures/tree/master/LazyEvaluation
PFDS 4章をOCamlに翻訳
PFDS 4章をOCamlに翻訳
PFDS 4章をOCamlに翻訳
Upcoming SlideShare
Loading in...5
×

PFDS 4章をOCamlに翻訳

875

Published on

Published in: Technology
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total Views
875
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

PFDS 4章をOCamlに翻訳

  1. 1. (* hogeは int lazy_t 型 *)let hoge = lazy 1
  2. 2. $ cat test_ocaml.mllet hoge = lazy 1 + 2$ omake*** omake: reading OMakefiles*** omake: finished reading OMakefiles (0.01 sec)- build . test_ocaml.cmi+ ocamlfind ocamlopt -package oUnit -warn-error A -annot -I . -c test_ocaml.mlFile "test_ocaml.ml", line 1, characters 11-17:Error: This expression has type int lazy_t but an expression was expected of type intlet hoge = (lazy 1) + 2
  3. 3. (* hogeは int lazy_t 型 *)let hoge = lazy (1 + 2)let rec map f s = lazy (match s with | (lazy SSnil) -> SSnil | (lazy (SScons(x, s))) -> SScons(f x, map f s))
  4. 4. $ cat test_ocaml.mllet hoge = lazy (1 + 2)let _ = print_int (Lazy.force hoge)(* (Lazy.force hoge) は Int 型 *)$ ./test_ocaml3
  5. 5. $ cat test_ocaml.mllet hoge = lazy (1 + 2)let _ = match hoge with | lazy i -> print_int i$ ./test_ocaml3
  6. 6. let plus (lazy m) (lazy n) = lazy (m + n)let _ = let i = Lazy.force (plus (lazy 1) (lazy 2)) in print_int i
  7. 7. fun lazy plus ($m, $n) = $m+nfun plus (x, y) = $case (x, y) of ($m, $n) => force ($m+n)(* plus : int lazy_t -> int lazy_t -> int lazy_t *)let plus m n = lazy(match (m, n) with | (lazy m, lazy n) -> m + n)
  8. 8. datatype a StreamCell = NIL | CONS of a * a Streamwithtype a Stream = a StreamCell susptype a cell = SSnil | SScons of a * a streamand a stream = a cell Lazy.t
  9. 9. fun lazy ($NIL) ++ t = t | ($CONS (x, s)) ++ t = $CONS (x, s ++ t)let rec (++) t1 t2 = lazy (match (t1, t2) with | (lazy SSnil, lazy t2) -> t2 | (lazy (SScons(x, s)), t2) -> SScons(x, s ++ t2))
  10. 10. fun lazy take (0, s) = $NIL | take (n, $NIL) = $NIL | take (n, $CONS (x, s)) = $CONS (x, take (n - 1, s))let rec take n s = lazy (match (n, s) with | (0, _) -> SSnil | (_, lazy SSnil) -> SSnil | (n, lazy (SScons(x, s))) -> SScons(x, take (n - 1) s))
  11. 11. fun lazy drop (n, s) = let fun drop (0, s) = s | drop (n, $NIL) = $NIL | drop (n, $CONS (x, s)) = drop (n - 1, s) in drop (n, s) endlet drop n s = lazy ( let rec drop n s = match (n, s) with | (0, lazy s) -> s | (_, lazy SSnil) -> SSnil | (n, lazy (SScons(_, s))) -> drop (n - 1) s in drop n s)
  12. 12. fun lazy reverse s = let fun reverse ($NIL, r) = r | reverse ($CONS (x, s), r) = reverse (s, $CONS (x, r)) in reverse (s, $NIL) endlet reverse s = lazy ( let rec reverse s r = match (s, r) with | (lazy SSnil, r) -> r | (lazy (SScons(x, s)), r) -> reverse s (lazy (SScons(x, r))) in Lazy.force (reverse s (lazy SSnil)))
  13. 13. https://github.com/master-q/readPurelyFunctionalDataStructures/tree/master/LazyEvaluation
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×