3

How can I define on scheme language this function that return if x>0 + else * like:

plus_or_muliti(int x) {
    if (x>0) return +;
    else return *;
}

i try this and it not work on racket:

(define (plus_or_multi x)
  (if (>= x 0) + *))

i got this error

+: expected a function call, but there is no open parenthesis before this function
Alex Knauth
  • 7,496
  • 2
  • 12
  • 26

2 Answers2

0

That code is perfectly valid in a full Scheme (in this case, Racket) implementation, but based on the error you're getting it looks like your using the Beginning Student Language, one of the several languages that the Racket system comes with. BSL is designed to help avoid some of the usual beginner mistakes when using Scheme-based languages, and all of the teaching-languages are designed to be used in tandem with the How to Design Programs book. In fact, a few libraries that come with Racket are in the htdp or htdp2 module, and derive from that book, even though they can be used for general purpose applications. In the DrRacket Language -> Choose Language menu, you can select the full version of the language, which will allow you to use the... full language.

Christopher Dumas
  • 1,220
  • 11
  • 27
-2

Your code is fine.

$ chibi-scheme
> (define (plus_or_multi x)
(if (>= x 0) + *))
> (plus_or_multi 5)
#<opcode "+">
> (plus_or_multi -2)
#<opcode "*">

It is not a Scheme interpreter, what you are using.

ceving
  • 16,775
  • 7
  • 82
  • 137
  • Yaniv Valotker is using Beginning Student Language, a variant of Scheme/Racket that is restricted so that it can help with more beginner mistakes. – Alex Knauth Apr 05 '17 at 16:55
  • Instead of helping beginners it confuses beginners, because it breaks the simplicity of Scheme. Principles are Scheme's greatest strengths. And what ever Racket is trying to do here is counterproductive. – ceving Nov 08 '17 at 08:36