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)))
      )
  • This is not lisp but scheme. Please tell us what software you are using, including version numbers. – sds Mar 27 at 3:00
  • I'm using DrRacket. I believe It's the 7.5 version – jjcode367 Mar 27 at 3: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 at 7:36
0

2020欧洲杯时间表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.

|improve this answer|||||
  • After removing the paren, I get this error message, "if: expected a question and two answers, but found only 2 parts" – jjcode367 Mar 27 at 3:20
  • @jjcode367: you had more than 1 extra paren. My code works, please try it. – sds Mar 27 at 3:23
  • I used your code and I get that error. Could it be the something with the setting in my DrRacket? – jjcode367 Mar 27 at 3:28
  • I guess DrRacket requires both alternatives for if - try the new version – sds Mar 27 at 3: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 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.