Last active
September 17, 2018 19:47
-
-
Save aike/b9ba60ca97abad93e839d6862514c133 to your computer and use it in GitHub Desktop.
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
;; (permutations {1 2 3}) ;=> {{1 2 3} {1 3 2} {2 1 3} {2 3 1} {3 1 2} {3 2 1}} | |
;; | |
(define $permutations | |
(lambda [$list] | |
(match list (multiset integer) | |
{[<nil> [{{}}]] | |
[<cons $x <nil>> [{{x}}]] | |
[<cons $x <cons $y <nil>>> {{x y} {y x}}] | |
[_ | |
[(foldr append {} | |
[(match-all list (multiset integer) | |
[<cons $x $xs> (map (lambda [$l] (cons x l)) (permutations xs))])])]] | |
}))) | |
;; (sublist {1 2 3}) ;=> {{1} {2} {3} {1 2} {1 3} {2 3} {1 2 3}} | |
;; | |
(define $sublist | |
(lambda [$list] | |
(cdr | |
(match-all list (multiset integer) | |
[<join $xs $ys> xs])))) | |
;; (sublist-permutations {1 2 3}) ;=> {{1} {2} {3} {1 2} {2 1} {1 3} {3 1} {2 3} {3 2} {1 2 3} {1 3 2} {2 1 3} {2 3 1} {3 1 2} {3 2 1}} | |
(define $sublist-permutations | |
(lambda [$l] | |
(unique (foldr append {} (map permutations (sublist l)))))) | |
;;;;;;;;;;;;;; 素数大富豪 ;;;;;;;;;;;;;;;;;;;;; | |
;; (prime-cards {1 6 13}) ;=> (prime? 1613) ;=> #t | |
(define $prime-cards? | |
(lambda [$cards] | |
(match cards (list integer) | |
{[,{} [#f]] | |
[,{1} [#f]] | |
[_ [(prime? (read (S.concat (map show cards))))]]}))) | |
(define $prime-cards-all | |
(lambda [$cards] | |
(filter prime-cards? | |
(sublist-permutations cards)))) | |
;;;;;;;;;;;;;; main ;;;;;;;;;;;;;;;;; | |
(define $main | |
(lambda [$args] | |
(let {[$num-args (map read args)]} | |
(print (show | |
(prime-cards-all num-args)))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see also https://github.com/egison/egison/blob/master/sample/prime-millionaire.egi