0

I'm new to programming and I'm learning the Scheme language using DrRacket version 7.5. I'm trying to create a function which takes in a number. The number should then be added to a value that is one number less than the one selected until it reaches 0. I believe I am trying to create an example of recursion.

(define (add-lesser-numbers num)
  (if(>= num 0))
     (+(num(- num 1)))
      )
sds
  • 58,617
  • 29
  • 161
  • 278
jjcode367
  • 25
  • 7
  • This is not lisp but scheme. Please tell us what software you are using, including version numbers. – sds Mar 27 '20 at 03:00
  • I'm using DrRacket. I believe It's the 7.5 version – jjcode367 Mar 27 '20 at 03:09
  • 1
    If the function were recursive, you would see its own name used in its definition. You want something of the form `(if (> num 0) recursion base-case)`. (Note that an `if` conditional must have both branches. DrRacket has an excellent documentation system; use it.) – molbdnilo Mar 27 '20 at 07:36

1 Answers1

0

One obvious glitch in your code is extra parentheses:

(define (add-lesser-numbers num)
  (if (>= num 0)
      (+ num (- num 1))
      (+ num 42)))

Remember, parens in lisp and scheme are meaningful.

sds
  • 58,617
  • 29
  • 161
  • 278
  • After removing the paren, I get this error message, "if: expected a question and two answers, but found only 2 parts" – jjcode367 Mar 27 '20 at 03:20
  • @jjcode367: you had more than 1 extra paren. My code works, please try it. – sds Mar 27 '20 at 03:23
  • I used your code and I get that error. Could it be the something with the setting in my DrRacket? – jjcode367 Mar 27 '20 at 03:28
  • I guess DrRacket requires _both_ alternatives for `if` - try the new version – sds Mar 27 '20 at 03:34
  • 1
    "expected a question and two answers"? This must be some toy for teaching toddlers. The three parts of a conditional are called *antecedent*, *consequent* and *alternative*. – Kaz Mar 27 '20 at 05:23