public
Last active

Factorial in Clojure without using alphanumeric characters

  • Download Gist
fact.swear.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
;; It all started here: http://clojure-log.n01se.net/date/2011-04-06.html#19:04
 
(#((% (+(*))) %) ;; call arg 1 with all args
[;; data
(+ (*)(*)(*)(*)(*)(*)(*))
;; main fn -- simulate 'if' with map lookup and closures
#(({(+) (% (+(*)(*)))} ;; if zero return 'then' clause
(% (+)) ;; dispatch on first arg
(% (+(*)(*)(*)))) ;; call 'else' clause (n not found in map)
%)
;; These clauses are given the same args as main fn.
;; base case ('then')
#({} % (+(*))) ;; constant fn of 1
;; recursive case ('else')
#(* ((% (+(*)))
[(- (% (+)) (+(*)))
(% (+(*)))
(% (+(*)(*)))
(% (+(*)(*)(*)))])
(% (+)))])
;; => 5040
 
 
;; Here's a simpler version that uses "if":
(#(% [% (+ (*)(*)(*)(*)(*)(*)(*))])
#(if (= (% (*)) (+))
(*)
(* ((% (+))
[(% (+)) (- (% (*)) (*))])
(% (*)))))
;; => 5040

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.