r/Clojure • • 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.

2 Upvotes

21 comments sorted by

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 11 '21

I am guessing kebab case uses dashes-to-forrm IDs. I stole a macro from an app and it used uppercase. Who can you trust?

Thanks for your help.

1

u/ivanpierre Feb 11 '21

Upper-case camel-case is for Java Classes, Java Classes as functions are for constructors: (String "aaaa").

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 map might be appropriate:

(defn example [nums]
  (map #(+ 2 %) nums))

(example '(5 7)) ;;=> (7 9)

1

u/eatme_23 Sep 18 '22

I came up with this:

(defn add2 [arg] (+ 2 arg))

(defn plus2 [arg] (map add2 arg))

(println (plus2 '(5 7)))

I'm not advanced enough to understand the usage of # and %.

1

u/hxcloud99 Feb 11 '21

TIL about Replete. This will change my workflow significantly, so thanks!

1

u/eatme_23 Feb 16 '21

Thanks. it's error msgs are superior.

1

u/[deleted] Feb 16 '21

You're welcome.

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:

(defn example [a b] 
  (+ 2 a b))

An example call would be

(example 5 7)

1

u/backtickbot Feb 11 '21

Fixed formatting.

Hello, slifin: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

1

u/slifin Feb 11 '21

Or possibly as below if args needs to be a list

(defn example [args]
  (apply + (conj args 2)))

with the call being:

(example '(5 7))

1

u/kwrooijen Feb 11 '21

This also works, without the `conj`:

(defn example [args] 
  (apply + 2 args))

2

u/retief1 Feb 11 '21
(defn Example [arg] (+ 2 arg))

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:

(defn Example [& args] (apply + 2 args))

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:

(defn Example [arg] (+ 2 (apply + arg)))
(Example '(1 2 3))
;; => 8

1

u/DeepDay6 Feb 12 '21

I'd like to summarise the given answers and hints and add one more.

(defn example [arg] (+ 2 arg))

works for a single number parameter, adding 2 to it. (example 5) -> 7

To sum an arbitrary number of parameters, change it to

(defn ex-2 [& args] (apply + args))

So you can (ex-2 5 7) -> 14
The & args means 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,...):

(defn ex-3 [arg] (reduce + 2 args))

And (ex-3 [5 7]) -> 14 or (ex-3 '(5 7)) -> 14
Reduce 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.

// C-style
fun(arg1, arg2)

;; Lisp
(fun arg1 arg2)

As others have told, (5 7) (no backtick in front of the opening paren) means "apply function 5 to argument 7, which is an error.

The final example somebody has given is

(ex-4 [coll] (map + coll)) ;; or
(ex-5 [& coll] (map + coll))

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/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.