16. モジュール分割
let rec quicksort = function
| [] -> []
| pivot :: rest ->
let is_less x = x < pivot in
let left, right = List.partition is_less rest in
quicksort left @ [pivot] @ quicksort right
17. モジュール分割
let rec quicksort = function
| [] -> []
| pivot :: rest ->
let is_less x = x < pivot in
let left, right = List.partition is_less rest in
quicksort left @ [pivot] @ quicksort right
List.map
Array.map
String.map
…など
21. local use-package
let average x y =
x Int64.(+) y Int64.(/) Int64.of_int 2;;
open Int64;;
let average x y =
x + y / of_int 2;;
let average x y =
let open Int64 in
x + y / of_int 2;;
let average x y =
let module I = Int64 in
x I.(+) y I.(/) I.of_int 2;;
22. local use-package
(defun average (x y)
let average x y =
(int64:/ (int64:+ x y)
x Int64.(+) y Int64.(/) Int64.of_int 2;;
(int64:of-int 2)))
open Int64;;
let average x y =
x + y / of_int 2;;
let average x y =
let open Int64 in
x + y / of_int 2;;
let average x y =
let module I = Int64 in
x I.(+) y I.(/) I.of_int 2;;
(use-package :int64)
(defun average (x y)
(/ (+ x y) (of-int 2)))
23. local use-package
(defun average (x y)
let average x y =
(int64:/ (int64:+ x y)
x Int64.(+) y Int64.(/) Int64.of_int 2;;
(int64:of-int 2)))
open Int64;;
let average x y =
x + y / of_int 2;;
(use-package :int64)
(defun average (x y)
(/ (+ x y) (of-int 2)))
(defun average (x y)
let average x y =
(with-use-packages (:int64)
let open Int64 in
(/ (+ x y) (of-int 2))))
x + y / of_int 2;;
let average x y =
(defun average (x y)
(with-package-nicknames
let module I = Int64 in
x I.(+) y I.(/) I.of_int 2;; ((i int64))
(i:/ (i:+ x y) (i:of-int 2))))
28. Variantとパターンマッチ
let rec quicksort = function
| [] -> []
| pivot :: rest ->
let is_less x = x < pivot in
let left, right = List.partition is_less rest in
quicksort left @ [pivot] @ quicksort right
type 'a list = 'a :: 'a list | []
29. Variantとパターンマッチ
let rec quicksort = function
| [] -> []
コンパイルエラー
| pivot :: rest ->
let is_less x = x < pivot in
let left, right = List.partition is_less rest in
quicksort left @ [pivot] @ quicksort right
type 'a list = 'a :: 'a list | []
30. Variantとパターンマッチ
let rec quicksort = function
コンパイルエラー
| left , right -> left :: [right]
| [] -> []
| pivot :: rest ->
let is_less x = x < pivot in
let left, right = List.partition is_less rest in
quicksort left @ [pivot] @ quicksort right
type 'a list = 'a :: 'a list | []
33. Common Lispでやってみた
赤黒木の例
type 'a tree =
| Leaf
| Red of tree * 'a * tree
| Black of tree * 'a * tree
(defvariant tree
leaf
(red tree t tree)
(black tree t tree))
gist https://gist.github.com/9225141 にサンプルコードあります
34. Common Lispでやってみた
赤黒木の例
type 'a tree =
| Leaf
| Red of tree * 'a * tree
| Black of tree * 'a * tree
(defvariant tree
leaf
(red tree t tree)
(black tree t tree))
(PROGN
(DEFTYPE TREE () '(OR (MEMBER LEAF) BLACK RED))
(DEFSTRUCT
(BLACK (:CONSTRUCTOR BLACK (#:TREE1014 #:T1015
#:TREE1016))
(:PRINT-OBJECT
(LAMBDA (OBJ STREAM)
(FORMAT STREAM "(~a ~{~a~^ ~})" 'BLACK
(LIST-SLOTS OBJ '(#:TREE1014 #:T1015 #:TREE1016)
(FORMAT NIL "~a-" 'BLACK))))))
(#:TREE1014 NIL :TYPE TREE)
(#:T1015 NIL :TYPE T)
(#:TREE1016 NIL :TYPE TREE))
(DEFPATTERN BLACK
(#:TREE1014 #:T1015 #:TREE1016)
(LIST 'BLACK- :TREE1014 #:TREE1014 :T1015 #:T1015 :TREE1016
#:TREE1016))
(DEFSTRUCT
(RED (:CONSTRUCTOR RED (#:TREE1017 #:T1018 #:TREE1019))
(:PRINT-OBJECT
(LAMBDA (OBJ STREAM)
(FORMAT STREAM "(~a ~{~a~^ ~})" 'RED
(LIST-SLOTS OBJ '(#:TREE1017 #:T1018 #:TREE1019)
(FORMAT NIL "~a-" 'RED))))))
(#:TREE1017 NIL :TYPE TREE)
(#:T1018 NIL :TYPE T)
(#:TREE1019 NIL :TYPE TREE))
(DEFPATTERN RED
(#:TREE1017 #:T1018 #:TREE1019)
(LIST 'RED- :TREE1017 #:TREE1017 :T1018 #:T1018 :TREE1019
#:TREE1019)))
gist https://gist.github.com/9225141 にサンプルコードあります