wiwo
wiwo

Reputation: 883

DrRacket define begin set! probably should work but dont

Im learning for exam of programming in lisp using DrRacket..

In presentation from lectures i found this code:

(define (f a b c)
    (define delta)
    (begin
        (set! delta (- (* b b) (* 4 a c)) 
        (if (>=? delta 0)
             (writeln ”są pierwiastki”)
             (writeln ”nie ma pierwiastków)))))

But it dont work. DrRacket is showing:

. define: bad syntax (missing expression after identifier) in: (define delta)

Can't I set delta value later?

What is the problem? thanks in advance

Upvotes: 0

Views: 186

Answers (3)

Sylwester
Sylwester

Reputation: 48745

I think Greg already has a perfect answer to your problem, but I just want to add the obvious let version of his code as well:

;; for a given ax^2+bx+c=0
;; this displays if it has at least one
;; real number answer or not
(define (equation-has-answer a b c)
  (let ((delta (- (* b b) (* 4 a c))))
    (if (>= delta 0)
        (displayln "Has answer(s)")
        (displayln "Has no answers"))))

To just make a predicate you can do this:

;; for a given ax^2+bx+c=0
;; this returns #t if it has at least one
;; real number answer and #f otherwise
(define (equation-has-answer? a b c)
  (>= (- (* b b) (* 4 a c)) 0))

Upvotes: 1

Óscar López
Óscar López

Reputation: 236034

Try this:

#lang racket

(define (f a b c)
  (define delta 0)
  (set! delta (- (* b b) (* 4 a c)))
  (if (>= delta 0)
      (displayln "są pierwiastki")
      (displayln "nie ma pierwiastków")))

What was wrong with your code:

  • It's probably a copy-paste error, but in Scheme we delimit strings using the " character, not as in your code. And the last line is missing the closing ".
  • Although some interpreters accept a define without an initial value, the standard is that a value must be present after the variable name
  • A closing parenthesis is missing at the end of the set! line
  • The define and set! lines can be merged into a single line: (define delta (- (* b b) (* 4 a c)))
  • The >=? operator is not standard in Scheme, it might work in your interpreter but if possible use the standard >=
  • The writeln procedure is not standard in Scheme, in Racket you can substitute it for displayln
  • Not really an error, but you don't need to write a begin inside a procedure definition, it's implicit

In conclusion: the code in the question seems to be intended for a different interpreter, this question was tagged racket but believe me, what you have is not valid Racket code - heck, is not even standard Scheme. Make sure to use the correct interpreter, and be very alert for typos in the text.

Upvotes: 2

Greg Hendershott
Greg Hendershott

Reputation: 16260

The original error message is because

(define delta)

is missing a value. Instead it should be something like:

(define delta 0)

There some other issues:

  • The double quotes weren't the " character and weren't recognized by Racket.

  • Some parens were wrong.

Also I don't know why it was define-ing delta, then immediately set!-ing it.

I tried to fix/simplify what you posted, and came up with the following. But I'm not really sure what the function is supposed to do, so I don't know if the example output is correct.

#lang racket
(define (f a b c)
  (define delta (- (* b b) (* 4 a c)))
  (if (>= delta 0)
      (displayln "są pierwiastki")
      (displayln "nie ma pierwiastków")))

;; Example output:
(f 1 2 3)
;;-> nie ma pierwiastków
(f 1 200 3)
;;-> są pierwiastki

Upvotes: 3

Related Questions