関数型プログラミングにおいて基本的なリスト操作関数を再帰を使わずに,単一のmatch-all式で定義できる. たとえば,map関数は以下のように定義できる.
(define $map
(lambda [$f $xs]
| concat (matchAll (["akira", "susumu", "tamotsu", "tomo"], ["blue", "green", "red", "white"], [1, 2, 3, 4]) | |
| as (list something, multiset something, multiset something) with | |
| | ([$x_1, $x_2, $x_3, $x_4], [$y_1, $y_2, $y_3, $y_4], [$z_1, $z_2, $z_3, $z_4]) | |
| -> matchAll [(x_1, y_1, z_1), (x_2, y_2, z_2), (x_3, y_3, z_3), (x_4, y_4, z_4)] | |
| as set (eq, eq, eq) with | |
| | (#"akira", #"blue", !#1) :: | |
| (#"susumu", !#"green", !#4) :: | |
| (_, #"red", #2) :: | |
| (_, #"white", $n) :: | |
| (#"tamotsu", _, #(n - 1)) :: _ |
| (take 6 (match-all primes (list integer) | |
| [<join _ <cons $p <cons ,(+ p 2) _>>> | |
| [p (+ p 2)]])) |
| % cat anagram.egi | |
| (define $anagram? | |
| (lambda [$xs $ys] | |
| (match ys (multiset eq) | |
| {[,xs #t] | |
| [_ #f]}))) | |
| (anagram? (between 1 31) (reverse (between 1 31))) | |
| (anagram? (between 1 31) (reverse (between 2 32))) | |
| % time egison -t anagram.egi |
| (filter (match-lambda (list something) | |
| {[(& <snoc ,D _> | |
| <join _ <cons ,B <join _ <cons ,C _>>>> | |
| <snoc _ <snoc _ <nioj _ <snoc ,B _>>>> | |
| <join _ <cons ,C <join _ <cons ,A _>>>>) | |
| #t] | |
| [_ #f]}) | |
| (match-all {A B C D} (multiset something) | |
| [<cons $a <cons $b <cons $c <cons $d <nil>>>>> {a b c d}])) |
| ;; | |
| ;; Tensor | |
| ;; | |
| [| x |] | |
| ;=> | |
| x | |
| (+ 1 [|1 2 3|]) | |
| ;=> |
| (define $∇ | |
| (lambda [$f $xs] | |
| (generate-tensor | |
| 1#(∂/∂ f (nth %1 xs)) | |
| {(length xs)}))) | |
| (define $nabla ∇) | |
| (define $taylor-expansion2 | |
| (lambda [$f $xs $as] |
| ;; Egison mode | |
| (load-file "~/egison/elisp/egison-mode.el") | |
| (autoload 'egison-mode "egison-mode" "Major mode for editing Egison code." t) | |
| (setq auto-mode-alist | |
| (cons `("\\.egi$" . egison-mode) auto-mode-alist)) | |
| (load-file "~/elisp/xah-math-input/xah-math-input.el") | |
| (global-set-key "\C-q" 'xah-math-input-change-to-symbol) |
% egison -T -e '(maclaurin-expansion (cos x) x)'
1
0
(/ (* -1 x^2) 2)
0
(/ x^4 24)
0
(/ (* -1 x^6) 720)
0
| cat number | egison -F1s -s '(match-all-lambda (list string) [<cons <join _ <cons $x <cons $y <join $s <cons ,x <cons ,y $t>>>>>> _> [x y (take-while eq? (zip (unpack s) (unpack t)))]])' |