Last active
November 17, 2023 01:47
-
-
Save righ1113/10ebfb2ac9d0ef5f2b10ac656abfb89e to your computer and use it in GitHub Desktop.
『ズンドコキヨシ with パターンマッチ指向言語Egison』をnew syntaxで
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- zundoko kiyoshi | |
-- | |
-- $ egison 4.1.3 | |
-- > loadFile "zdk.egi" | |
-- > zdk | |
-- | |
-- Codes | |
-- | |
def zdk := | |
match (map 1#(sample ["ズン", "ドコ"]) nats) as list string with | |
| $before ++ (loop $i (1, 4) (#"ズン" :: ...) (#"ドコ" :: _)) | |
-> before ++ ["ズン", "ズン", "ズン", "ズン", "ドコ", "キ・ヨ・シ!"] | |
-- | |
-- Tests | |
-- | |
def test := | |
match ["bar", "ズン", "ズン", "ズン", "ズン", "ドコ", "hoo"] as list string with | |
| $before ++ (loop $i (1, 4) (#"ズン" :: ...) (#"ドコ" :: _)) -> before ++ ["ズン", "ズン", "ズン", "ズン", "ドコ"] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 参照ページ:https://qiita.com/greymd/items/222930a528fba197a126 | |
;; > egison -N もしくは egison -N zundoko-N.egi | |
;; > loadFile("zundoko-N.egi") | |
;; > io(main()) | |
;; > io(each(print, map(show, take(10, primes)))) | |
main(args) = | |
let input = | |
matchAll map(1#R.car(["ズン", "ドコ"]), nats) as list(string) | |
| _ <++> loop($i, (1, [5], _), <cons $z_i ...>, _) -> [z_1, z_2, z_3, z_4, z_5] | |
in each(print, kiyoshi(input)) | |
kiyoshi = | |
matchLambda as list(list(string)) | |
| (loop($i, (1, [4], _), <cons "ズン" ...>, <cons "ドコ" <nil>>) and $zs) <:> _ -> [@zs, "キ・ヨ・シ !"] | |
| ($x<:>_) <:> $xs -> [x, @kiyoshi(xs)] | |
;; ------------おまけ-------------- | |
fizzbuzz' = | |
matchLambda as mod(15) | |
| 0 -> "FizzBuzz" | |
| (3 or 6 or 9 or 12) -> "Fizz" | |
| (5 or 10) -> "Buzz" | |
| $x -> x | |
fizzbuzz(n) = map(fizzbuzz', take(n, nats)) | |
;; -------------------------- | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 参照ページ:https://qiita.com/greymd/items/222930a528fba197a126 | |
-- > egison | |
-- > loadFile "zundoko.egi" | |
-- > io $ main () | |
-- ------------ おまけ -------------- | |
def fizzbuzz' := | |
\match as (mod 15) with | |
| #0 -> "FizzBuzz" | |
| (#3 | #6 | #9 | #12) -> "Fizz" | |
| (#5 | #10) -> "Buzz" | |
| $x -> x | |
def fizzbuzz n := map fizzbuzz' $ take n nats | |
-- ---------------------------------- | |
def kiyoshi := | |
\match as (list (list string)) with | |
| ((loop $i (1, 4) (#"ズン" :: ...) (#"ドコ" :: [])) & $zs) :: _ -> zs ++ ["キ・ヨ・シ !"] | |
| ($x :: _) :: $xs -> [x] ++ kiyoshi xs | |
def main args := | |
each print $ kiyoshi $ | |
matchAll (map 1#(sample ["ズン", "ドコ"]) nats) as list string with | |
| _ ++ (loop $i (1, 5) ($z_i :: ...) _) -> [z_1, z_2, z_3, z_4, z_5] | |
def test := [["ズン", "ズン", "ズン", "ズン", "ドコ"]] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment