Skip to content

Instantly share code, notes, and snippets.

@timmc
Created July 2, 2012 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timmc/3036114 to your computer and use it in GitHub Desktop.
Save timmc/3036114 to your computer and use it in GitHub Desktop.
Factorial in Clojure without using alphanumeric characters
(#(% [% (+ (*)(*)(*)(*)(*)(*)(*))])
#(if (= (% (*)) (+))
(*)
(* ((% (+))
[(% (+)) (- (% (*)) (*))])
(% (*)))))
;; => 5040
;; Almost there, just an "if" to get rid of...
(#((% (+(*))) %) ;; 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
;; That does it!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment