Skip to content

Instantly share code, notes, and snippets.

@aike
Last active September 17, 2018 19:47
Show Gist options
  • Save aike/b9ba60ca97abad93e839d6862514c133 to your computer and use it in GitHub Desktop.
Save aike/b9ba60ca97abad93e839d6862514c133 to your computer and use it in GitHub Desktop.
;; (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))))))
@aike
Copy link
Author

aike commented Sep 17, 2018

@aike
Copy link
Author

aike commented Sep 17, 2018

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment