Hannah and Je together

Monday, 26 July 2010

The Art of LISP Programming 01

Filed under: Lang:English,Subj:GNU/Computer — Jemyoung Leigh @ 21:04
Tags: , ,

I am studying LISP using the book named “The Art of LISP Programming (1990)” printed in Great Britain, and I will post the Exercises of each chapter.

The funny thing is that this book was classified as “Fiction!”

  1. Define a function sq(x) which returns the square of x.
  2. Using sq, define a function diff(x y) which returns x^2-y^2.
  3. According to elementary algebra, x^2-y^2 = (x + y)(x – y). How do you write the right-hand side in LISP?
  4. Define a function called test(x y) which returns 0 if diff(x y) is equal to (x+y)(x-y).
  5. What is the biggest product of two integers which your LISP implementation will evaluate correctly? Why do you think this is?
  6. The ancient Babylonians computed products xy by using tables of squares and the identity:
4xy = (x + y)^2 - (x-y)^2                          (1)

which implies:

xy = ((x+y)^2 - (x-y)^2)/4                         (2)

Write a function forprod(x y) to evaluate the right-hand side of (1) using only sq and the arithmetic primitives. Use it to write a function babyprod(x y) that evaluates the right-hand side of (2). Write a function check(x y) that devides (babyprod x y) by xy, and use it to verify that (babyprod 12 13) = 156.

OK, here go my answers:

(1)

(defun sq (x)
(* x x)
)

(2)

(defun diff (x y)
(- (sq x) (sq y))
)

(3)

(* (+ x y) (- x y))

(4)

(defun test (x y)
(- (diff x y) (* (+ x y) (- x y)))
)

(5) I don’t know.

(6)

(defun forprod (x y)
(- (sq (+ x y)) (sq (- x y)))
)
(defun babyprod (x y)
(/ (forprod x y) 4)
)
(defun check (x y)
(/ (babyprod x y) (* x y))
)
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: