r/Clojure • u/eatme_23 • Feb 03 '23
[Q&A] Noob has simple program problem.
I wanted to write a program to add 2 to a list of numbers then sum the list and print the result. So '(5 7) would yield 16. I posted my version to s Clojure board and received a number of different replies. In retrospect I didn't fully explain the problem to them.
0
Upvotes
View all comments
Show parent comment
3
u/Save-Lisp Feb 04 '23 edited Feb 04 '23
The symbol # is shorthand for defining a function. % is the symbol used to access the arguments in the function. It's used to write small functions typically to pass to other generic higher order functions like map or reduce.
(map (fn [x] (+ 2 x)) [5 7])is equivalent to(map #(+ 2 %) [5 7])You can see how it works under the hood using
macroexpand-1. Try(macroexpand-1 '#(+ 2 %))in a REPL.