Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;Kitty
- ;First, we're going to be using the discriminant a lot, so let's code a function to calculate that. Leave out
- ;the sqrt, because discriminant means just the (b^2)-4ac underneath the root.
- (define disc (lambda (a b c)
- (- (* b b)(* 4 a c))))
- ;We want a function that tells us about the roots. There are three possibilities:
- ;0 roots, 1 root, 2 rational roots, 2 irrational roots. That means we need three cases in our conditional.
- ;(define rootsOfQuadratic (lambda (a b c)
- ; (cond (() "No real roots")
- ; (() "One root")
- ; (() "Two roots"))
- ;There's our four cases, so now we fill in the conditions that make them true.
- (define rootsOfQuadratic (lambda (a b c)
- (cond ((< (disc a b c) 0) "No real roots")
- ((= (disc a b c) 0) "One root")
- ((> (disc a b c) 0) "Two roots"))))
- ;The second part is to extend the function to determine whether or not, in the case of two roots,
- ;if the roots are rational or not. To do this, we branch out the cond statement.
- ;(define rootsOfQuadratic2 (lambda (a b c)
- ; (cond ((< (disc a b c) 0) "No real roots")
- ; ((= (disc a b c) 0) "One root")
- ; ((> (disc a b c) 0) (cond (() "Two rational roots")
- ; (() "Two irrational roots"))))))
- ;And now we fill in the conditions.
- (define rootsOfQuadratic2 (lambda (a b c)
- (cond ((< (disc a b c) 0) "No real roots")
- ((= (disc a b c) 0) "One root")
- ((> (disc a b c) 0) (cond ((Integer? (disc a b c)) "Two rational roots")
- ((Integer? (disc a b c)) "Two irrational roots"))))))
- ;That's a hell of a lot of parenthesis.
Add Comment
Please, Sign In to add comment