Skip to content

Instantly share code, notes, and snippets.

View egisatoshi's full-sized avatar

Satoshi Egi egisatoshi

View GitHub Profile
@egisatoshi
egisatoshi / Multiset.egi
Created July 17, 2012 14:09
Multiset Definition in Egison 2.3
(define $Multiset
(lambda [$a]
(type
{[,$val []
{[$tgt (match [val tgt] [(Multiset a) (Multiset a)]
{[[<nil> <nil>] {[]}]
[[<cons $x $xs> <cons ,x ,xs>] {[]}]
[[_ _] {}]})]}]
[<nil> []
{[{} {[]}]
@egisatoshi
egisatoshi / poker-hands.egi
Created July 17, 2012 14:37
Poker Hands in Egison
(define $poker-hands
(lambda [$Cs]
(match Cs (Multiset Card)
{[<cons <card $S $n>
<cons <card ,S ,(- n 1)>
<cons <card ,S ,(- n 2)>
<cons <card ,S ,(- n 3)>
<cons <card ,S ,(- n 4)>
!<nil>>>>>>
<straight-flush>]
@egisatoshi
egisatoshi / execute mine.egi
Created July 20, 2012 08:17
icfpc2012 simulator in Egison2.3.3
% cd sample/icfpc2012/
% egisonc mine.egi
[1 of 1] Compiling Main ( _tmp.hs, _tmp.o )
Linking mine ...
% ./mine maps/contest1.map
######
#. *R#
# \.#
#\ * #
L .\#
@egisatoshi
egisatoshi / Set
Last active October 10, 2015 07:18
Set cons of Egison from version 2.4.2
> (test (match-all {0 1} (Set Integer)
[<cons $a_1 <cons $a_2 <cons $a_3 _>>>
{a_1 a_2 a_3}]))
{{0 0 0} {0 0 1} {0 1 0} {0 1 1} {1 0 0} {1 0 1} {1 1 0} {1 1 1}}
@egisatoshi
egisatoshi / mah-jong
Last active December 13, 2015 22:19
Mah-jong in next Egison
;
; Mah-jong example
;
(define $shuntsu : (PatternConstructor [Hai (Collection Hai)] (Collection Hai))
(pattern-constructor [$pat1 $pat2]
<cons (& <num $s $n> pat1)
<cons <num ,s ,(+ n 1)>
<cons <num ,s ,(+ n 2)>
pat2>>>))
@egisatoshi
egisatoshi / twin primes
Created July 8, 2013 14:54
Playing with prime numbers with Egison
/home/egi/egison3% egison [0]
Egison Version 3.0.8 (C) 2011-2013 Satoshi Egi
http://egison.pira.jp
Welcome to Egison Interpreter!
> (take 10 (match-all primes (list integer) [<join _ <cons $n <cons ,(+ n 2) _>>> [n (+ n 2)]]))
{[3 5] [5 7] [11 13] [17 19] [29 31] [41 43] [59 61] [71 73] [101 103] [107 109]}
> (take 10 (match-all primes (list integer) [<join _ <cons $n <cons ,(+ n 4) _>>> [n (+ n 4)]]))
{[7 11] [13 17] [19 23] [37 41] [43 47] [67 71] [79 83] [97 101] [103 107] [109 113]}
> (take 1 (match-all primes (list integer) [<join _ <cons $n <cons ,(+ n 2) <cons ,(+ n 4) _>>>> [n (+ n 2) (+ n 4)]]))
{[3 5 7]}
@egisatoshi
egisatoshi / prime triplet
Created July 8, 2013 15:03
Playing with prime numbers
/home/egi/egison3% egison [130]
Egison Version 3.0.8 (C) 2011-2013 Satoshi Egi
http://egison.pira.jp
Welcome to Egison Interpreter!
> (take 5 (match-all primes (list integer) [<join _ <cons $n <cons ,(+ n 2) <cons ,(+ n 6) _>>>> [n (+ n 2) (+ n 6)]]))
{[5 7 11] [11 13 17] [17 19 23] [41 43 47] [101 103 107]}
> (take 5 (match-all primes (list integer) [<join _ <cons $n <cons ,(+ n 4) <cons ,(+ n 6) _>>>> [n (+ n 2) (+ n 6)]]))
{[7 9 13] [13 15 19] [37 39 43] [67 69 73] [97 99 103]}
>
Leaving Egison Interpreter.
> (take 10 (match-all nats (list integer) [<join _ (& <cons $x _> <join _ (& <cons $y _> <join _ <cons (& ?(lambda [$z] (eq? (* z z) (+ (* x x) (* y y)))) $z) _>>)>)> [x y z]]))
{[3 4 5] [6 8 10] [5 12 13] [9 12 15] [8 15 17] [12 16 20] [7 24 25] [15 20 25] [10 24 26] [20 21 29]}
> (take 5 (match-all primes (list integer) [<join _ <cons $m <cons (& ?(gte-i? $ (+ 50 m)) $n) _>>> [m n]]))
{[19609 19661] [25471 25523] [31397 31469] [31907 31957] [34061 34123]}
@egisatoshi
egisatoshi / new feature version 3.2.0
Last active January 2, 2016 02:28
From Egison version 3.2.0, the syntax of loop pattern is changed. The following code is an example.
% egison
Egison Version 3.2.0 (C) 2011-2013 Satoshi Egi
http://www.egison.org
Welcome to Egison Interpreter!
> (match-all {1 2 3 4 5} (list integer) [(loop $i [1 2] <join _ <cons $a_i ...>> _) a])
{[|1 2|] [|1 3|] [|2 3|] [|1 4|] [|2 4|] [|3 4|] [|1 5|] [|2 5|] [|3 5|] [|4 5|]}
> (match-all {1 2 3 4 5} (list integer) [(loop $i [1 $n] <cons $a_i ...> _) [a n]])
{[[||] 0] [[|1|] 1] [[|1 2|] 2] [[|1 2 3|] 3] [[|1 2 3 4|] 4] [[|1 2 3 4 5|] 5]}
> (match-all {1 2 3 4 5} (list integer) [(loop $i [1 $n] <join _ <cons $a_i ...>> _) [a n]])
{[[||] 0] [[|1|] 1] [[|2|] 1] [[|3|] 1] [[|4|] 1] [[|5|] 1] [[|1 2|] 2] [[|1 3|] 2] [[|2 3|] 2] [[|1 4|] 2] [[|2 4|] 2] [[|3 4|] 2] [[|1 5|] 2] [[|2 5|] 2] [[|3 5|] 2] [[|4 5|] 2] [[|1 2 3|] 3] [[|1 2 4|] 3] [[|1 3 4|] 3] [[|2 3 4|] 3] [[|1 2 5|] 3] [[|1 3 5|] 3] [[|1 4 5|] 3] [[|2 3 5|] 3] [[|2 4 5|] 3] [[|3 4 5|] 3] [[|1 2 3 4|] 4] [[|1 2 3 5|] 4] [[|1 2 4 5|] 4] [[|1 3 4 5|] 4] [[|2 3 4 5|] 4] [[|1 2 3 4 5|] 5]}