1

Im want to make a function where rootcheck has a list L as input, L always is 3 atoms (a b c) where a is coefficient of x^2, b coef of x and c is the constant. it checks if the equation is quadratic, using discriminant (b^2 - 4ac) and should output this (num 'L) where num is the number of roots and L is a list that contains the roots themselves (using quadratic formula), L is empty in case of no roots. here is my code:

(define roots-2
    (lambda (L)
    (let ((d (- (* (cdr L) (cdr L)) (4 (car L) (caddr L))))))
    (cond ((< d 0)  (cons(0 null)))
    ((= d 0) (cons(1 null)))
    (else((> d 0) (cons(2 null)))))
            ))

its giving me no expression in body error.

also I tried to code the quadratic function and even tried some that are online, one compiled fint but gave me an error when I inserted input this is the code for the quadratic function, NOT MINE!

(define quadratic-solutions
 (lambda (a b c) (list (root1 a b c) (root2 a b c))))
(define root1
 (lambda (a b c) (/ (+ (- b) (sqrt (discriminant a b c)))
 (* 2 a))))
(define root2
 (lambda (a b c) (/ (- (- b) (sqrt (discriminant a b c)))
 (*2 a)))) 
(define discriminant
 (lambda (a b c) (- (square b) (* 4 (* a c)))))
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
user3241846
  • 643
  • 3
  • 9
  • 26

2 Answers2

3

There are several mistakes in the code:

  • Some parentheses are incorrectly placed, use a good IDE to detect such problems. This is causing the error reported, the let doesn't have a body
  • You forgot to multiply in the 4ac part
  • You're incorrectly accessing the second element in the list
  • The else part must not have a condition
  • The output list is not correctly constructed

This should fix the errors, now replace null with the actual call to the function that calculates the roots for the second and third cases (the (< d 0) case is fine as it is):

(define roots-2
  (lambda (L)
    (let ((d (- (* (cadr L) (cadr L)) (* 4 (car L) (caddr L)))))
      (cond ((< d 0)  (list 0 null))
            ((= d 0)  (list 1 null))
            (else     (list 2 null))))))
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Thanks!! im using DrRacket, any recommendation? – user3241846 Mar 14 '14 at 01:45
  • 2
    Use the "Reindent All" option with frequency, it's in the Racket menu. And do a "Check Syntax" every now and then, it'll help you catch errors early. Write little pieces of code and test them constantly. Let the IDE guide you, indent and close parentheses in the standard way - all of the above will help you! – Óscar López Mar 14 '14 at 02:03
0

for the quadractic function part, I found a code online and tweaked it to provide both roots of a quadratic equation. returns a list of both roots

(define (solve-quadratic-equation a b c)
  (define disc (sqrt (- (* b b)
                    (* 4.0 a c))))

 (list      (/ (+ (- b) disc) (* 2.0 a))
            (/ (- (- b) disc) (* 2.0 a))
))
user3241846
  • 643
  • 3
  • 9
  • 26