Skip to content

Instantly share code, notes, and snippets.

View egisatoshi's full-sized avatar

Satoshi Egi egisatoshi

View GitHub Profile
% 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

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

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

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

(define $map
 (lambda [$f $xs]
(take 6 (match-all primes (list integer)
[<join _ <cons $p <cons ,(+ p 2) _>>>
[p (+ p 2)]]))
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)) :: _