kfem
kfem

Reputation: 189

testing functions with unexpected output

I have a program written to compute derivatives of expressions in list form so 4x^3 is represented as (* 4 (expt x 3)) and I am writing a test-suite to print out results. I have a function runtest that takes an expression and computes the derivative with respect to x and returns the results

(define (runtest test)
  (display "diff x ")(display test)(display " => ")(display (diff 'x test)))

so that the results display diff x expression => result

I have the following defined as tests:

(define test1 '4)
(define test2 '(* 2 x))
(define test3 '(* 2 y))
(define test4 '(+ x (* x x)))
(define test5 '(expt x 4))

and then I put all of the tests in a list so I could map over the list and return the results:

(define test-suite (list test1 test2 test3 test4 test5))

and when I run (map runtest test-suite) instead of returning each separately I get a long list with no line breaks when instead I want:

diff x 4 => 0
diff x '(* 2 x) => '(+ (* 0 x) (* 2 1))
diff x '(* 2 y) => '(+ (* 0 y) (* 2 0))
diff x '(+ x (* x x)) => '(+ 1 (* 2 x))
diff x '(expt x 4) => '(* 4 (expt x 3))

what am I missing?

Upvotes: 1

Views: 99

Answers (1)

Ryan Culpepper
Ryan Culpepper

Reputation: 10643

I think you're looking for newline:

(define (runtest test)
  (display "diff x ")
  (display test)
  (display " => ")
  (display (diff 'x test))
  (newline))

BTW, there's seldom a good reason to call display on anything but a string; I would recommend using write for test and (diff 'x test).

A more idiomatic way to write it in Racket is this:

(define (runtest test)
  (printf "diff x ~s => ~s\n" test (diff 'x test)))

or, in any Scheme with SRFI-28, this:

(define (runtest test)
  (display (format "diff x ~s => ~s~%" test (diff 'x test)))

Upvotes: 4

Related Questions