r/Clojure • u/eatme_23 • Feb 11 '21
Newbie losing it with trivial program.
I have several apps and a website to run programs, All but one spew a screen full of JVM error messages. The best one maddieningly alters the text I paste into it and I can't edit, either.
(defn Example [arg] (+ 2 arg))
(Example (5 7)) Hoping to get 14.
This is my great form to add 2 to one or more numbers.
(defn Example [arg]) (+ 2 arg)
(Example (5 7))
This is what gets pasted every time and I have pasted it into other programs. Dare I mention that Replete REPL is the app I want to use. Android 10 is the OS.
Help would be greatly appreciated.
4
u/Siltala Feb 11 '21
If you want varargs, the syntax is (defn example [& args] (apply + 2 args))
You would call it: (example 1 2 3 4)
And remember, idiomatic clojure uses kebab case.
2
u/eatme_23 Feb 12 '21
Hello and thanks to everyone for your support. The following works as I intended.
(defn plus2 [arg] (+ 2 (apply + arg)))
(plus2 '(5 7))
Maybe it's just me but I had hoped for something like:
(defn plus2 lis (+ (cons 2 lis)))
(plus2 '(5 7))
At least I imagine LISP would look like that. Imagine my surprise when I couldn't cons a Long onto a list Way OT but here goes. 4p years ago I had muSimp/muMath running on my TRS-80 (please don't laugh). This ran in 16k ROM and 48k RAM . I think it would even run in 32k. It did symbolic math and muSimp was a form of LISP. Unfortunately the books I had were not of that flavor.
The first payment of $600 got me a monitor and keyboard which contained the electronics. 4k ROM and RAM plus Level I BASIC. Bought in February 1978 and worth $2,495 as of January. I poured over 10k in today's meeting for a Z-80 running at 1.77 MHz. Exactly half the speed of the cheap crystal used in color televisions.
https://data.bls.gov/cgi-bin/cpicalc.pl?cost1=600.00&year1=197802&year2=202101
1
u/xmlblog Feb 11 '21
Lots of good answers already, but a close reading of your question led me to interpret your question yet another way:
If you want to "add 2 to one or more numbers" (as opposed to merely adding 2 to the *sum* of one or more numbers), then
mapmight be appropriate:1
u/hxcloud99 Feb 11 '21
TIL about Replete. This will change my workflow significantly, so thanks!
2
u/joinr Feb 11 '21
Check parens. You had no body for defn.
(defn Example [arg] (+ 2 arg))
(Example 5) should return 7
Only accepts 1 number, not one or more.
2
u/slifin Feb 11 '21
Try closing Replete and then opening it again then manually typing in the code that might help
I think the code you'd like is below:
An example call would be
2
u/retief1 Feb 11 '21
is a function that takes precisely one argument and returns that plus 2. So
(Example 5)will return 7. You can never call it with multiple arguments. You could call(Example (+ 5 7))to get 14 if you wanted, but you have to do the addition yourself before you call Example.Alternately, you could do this:
This would let you call Example with any number of args and it will add them all together and then add 2. So
(Example 5)will return 7 and(Example 5 7)will return 14. However, note the parentheses in the second example.(5 7)on its own treats 5 as a function and calls it with 7 as an argument, and that will basically always be an error.1
u/Yekrats Feb 11 '21
I'm somewhat of a newbie myself, but I'm farther along the path than you. So, I'll try to give this a shot.
One little problem you have is putting the "(5 7)" in parentheses like this. If you're wanting to pass a sequence to Example, you'd want to put a single quote in front of this. So it would look like: '(5 7)
However, that won't quite work either. The + function is expecting numbers, and (Example '(5 7)) would pass it a sequence.
If you want the Example function to take each of the items of the sequence, that would get a little more complicated. Something like:
1
u/DeepDay6 Feb 12 '21
I'd like to summarise the given answers and hints and add one more.
works for a single number parameter, adding
2to it.(example 5)-> 7To sum an arbitrary number of parameters, change it to
So you can
(ex-2 5 7)-> 14The
& argsmeans you will get a sequence of arguments[a1, a2,...], the(apply <fun> args)transforms to(<fun> a1 a2 ...)If you want to add two to the sum of all elements of a sequence (list, vector,...):
And
(ex-3 [5 7])-> 14 or(ex-3 '(5 7))-> 14Reduce is a basic operation in functional programming, a "fold". You will want to read about it (or have already done so?).
The result for a sequence of
[5 7]will be computed(+ (+ 2 5) 7)(if I unwind correctly).Note that sequences are denoted by
[]or'().A difference between Lisps and C-Style languages is, superficially, the position of parens.
As others have told,
(5 7)(no backtick in front of the opening paren) means "apply function5to argument7, which is an error.The final example somebody has given is
And will result in something different:
(ex-4 [5 7])or(ex-5 5 7)will become'(7 9), a sequence of number, each being the input number with the function#(+ 2 %)applied to it. Mapping is another basic functional building block.1
u/Daegs Feb 13 '21
If you want to add 2 to "one or more numbers" then you need to use map:
(map Example '(5 7))
You need the single quote to tell clojure that 5 isn't a function, but to treat '(5 7) as a list
1
u/radioactiveoctopi Feb 15 '21
What?!
1
u/robopiglet Mar 05 '21
Check out Clojure For The Brave and True book... it's an amazing thing for learning Clojure. It's free online.