Question

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)))))
Was it helpful?

Solution

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))))))

OTHER TIPS

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))
))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top