Skip to content

Instantly share code, notes, and snippets.

View egisatoshi's full-sized avatar

Satoshi Egi egisatoshi

View GitHub Profile
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)]]))

Egisonプログラミングコンテスト2018問題集

パターンマッチ指向プログラミング

関数型プログラミングにおいて基本的なリスト操作関数を再帰を使わずに,単一のmatch-all式で定義できる. たとえば,map関数は以下のように定義できる.

(define $map
 (lambda [$f $xs]
% 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}]))
@egisatoshi
egisatoshi / tensor.egi
Last active May 19, 2016 07:16
tensor design
;;
;; Tensor
;;
[| x |]
;=>
x
(+ 1 [|1 2 3|])
;=>
@egisatoshi
egisatoshi / taylor-expansion.egi
Created May 17, 2016 01:57
Multi variate Taylor Expansion
(define $∇
(lambda [$f $xs]
(generate-tensor
1#(∂/∂ f (nth %1 xs))
{(length xs)})))
(define $nabla ∇)
(define $taylor-expansion2
(lambda [$f $xs $as]
@egisatoshi
egisatoshi / emacs_egison.el
Last active August 4, 2017 01:22
emacs seting for Egison
;; 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)
@egisatoshi
egisatoshi / maclaurin-expansion.md
Last active April 14, 2016 06:23
MacLaurin expansion on shell
% egison -T -e '(maclaurin-expansion (cos x) x)'                                        
1
0
(/ (* -1 x^2) 2)
0
(/ x^4 24)
0
(/ (* -1 x^6) 720)
0
@egisatoshi
egisatoshi / shell18.egi
Last active September 2, 2015 02:01
shell game 18 q8
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)))]])'