[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/l2YWpGaf    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Mar 15:
; sum of squares of two largest of three values

(define-syntax assert
  (syntax-rules ()
    ((assert expr result)
      (if (not (equal? expr result))
          (for-each display `(
            #\newline "failed assertion:" #\newline
            expr #\newline "expected: " ,result
            #\newline "returned: " ,expr #\newline))))))

(define (test f)
  (assert (f 3 4 5) 41)
  (assert (f 3 5 4) 41)
  (assert (f 4 3 5) 41)
  (assert (f 4 5 3) 41)
  (assert (f 5 3 4) 41)
  (assert (f 5 4 3) 41)
  (assert (f 3 3 4) 25)
  (assert (f 3 4 3) 25)
  (assert (f 4 3 3) 25)
  (assert (f 3 4 4) 32)
  (assert (f 4 3 4) 32)
  (assert (f 4 4 3) 32)
  (assert (f 3 3 3) 18))

(define (f x y z)
  (if (< x y)
      (if (< x z)
          (+ (* y y) (* z z))
          (+ (* x x) (* y y)))
      (if (< y z)
          (+ (* x x) (* z z))
          (+ (* x x) (* y y)))))

(display "First function")
(test f)
(newline)

(define (f x y z)
  (if (= x (min x y z))
      (+ (* y y) (* z z))
      (f y z x)))

(display "Second function")
(test f)
(newline)

(define (f x y z) 
 (cond ((and (< x y) (< x z)) (+ (* y y) (* z z))) 
       ((and (< y x) (< y z)) (+ (* x x) (* z z))) 
       (else (+ (* x x) (* y y)))))

(display "Third function")
(test f)


Output:
1
2
3
4
5
6
7
First function
Second function
Third function
failed assertion:
(f 3 3 4)
expected: 25
returned: 18


Create a new paste based on this one


Comments: