Guest User

Untitled

a guest
Dec 6th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.66 KB | None | 0 0
  1. ;Kitty
  2.  
  3. ;First, we're going to be using the discriminant a lot, so let's code a function to calculate that. Leave out
  4. ;the sqrt, because discriminant means just the (b^2)-4ac underneath the root.
  5.  
  6. (define disc (lambda (a b c)
  7.     (- (* b b)(* 4 a c))))
  8.  
  9. ;We want a function that tells us about the roots. There are three possibilities:
  10. ;0 roots, 1 root, 2 rational roots, 2 irrational roots. That means we need three cases in our conditional.
  11.  
  12. ;(define rootsOfQuadratic (lambda (a b c)
  13. ;    (cond (() "No real roots")
  14. ;          (() "One root")
  15. ;          (() "Two roots"))
  16.  
  17. ;There's our four cases, so now we fill in the conditions that make them true.
  18.  
  19. (define rootsOfQuadratic (lambda (a b c)
  20.     (cond ((< (disc a b c) 0) "No real roots")
  21.           ((= (disc a b c) 0) "One root")
  22.           ((> (disc a b c) 0) "Two roots"))))
  23.  
  24. ;The second part is to extend the function to determine whether or not, in the case of two roots,
  25. ;if the roots are rational or not. To do this, we branch out the cond statement.
  26.  
  27. ;(define rootsOfQuadratic2 (lambda (a b c)
  28. ;    (cond ((< (disc a b c) 0) "No real roots")
  29. ;          ((= (disc a b c) 0) "One root")
  30. ;          ((> (disc a b c) 0) (cond (() "Two rational roots")
  31. ;                                    (() "Two irrational roots"))))))
  32.  
  33. ;And now we fill in the conditions.
  34.  
  35. (define rootsOfQuadratic2 (lambda (a b c)
  36.     (cond ((< (disc a b c) 0) "No real roots")
  37.           ((= (disc a b c) 0) "One root")
  38.           ((> (disc a b c) 0) (cond ((Integer? (disc a b c)) "Two rational roots")
  39.                                     ((Integer? (disc a b c)) "Two irrational roots"))))))
  40.  
  41. ;That's a hell of a lot of parenthesis.
Add Comment
Please, Sign In to add comment