## Macros: Difficult **Solutions**

### code writing

Manhattan distance between two points is defined as the sum of the vertical and horizontal distances between the two points. More formally, the manhattan distance between points (x1, y1) and (x2, y2) is |x1 - x2| + |y1 - y2|. Write a macro that calculates the manhattan distance between two points **p1** and **p2** which are represented as two element lists.

> (define p1 '(1 2))

> (define p2 '(3 4))

> (manhattan-dist p1 p2)

4

(define-macro (manhattan-dist p1 p2)

**`(let ((x1 (car ,p1))
(y1 (car (cdr ,p1)))
(x2 (car ,p2))
(y2 (car (cdr ,p2))))
(+ (abs (- x1 x2)) (abs (- y1 y2))))**

)

Let's break this down into steps to make the solution more clear. The first step is to write out the expression that you want your macro to construct. We're going to use a let statement to solve this problem, because it's the simplest way to do it.

(let ((x1 (car p1))

(y1 (car (cdr p1)))

(x2 (car p2))

(y2 (car (cdr p2))))

(+ (abs (- x1 x2)) (abs (- y1 y2))))

Now let's construct the list that creates that expression. I'm going to do this using quasiquote/unquote, but you can also do this using the list/quote method as well. Remember that we want to unquote the parameters (p1 and p2) so that we get their actual values as opposed to the symbols p1 and p2.

`(let ((x1 (car ,p1))

(y1 (car (cdr ,p1)))

(x2 (car ,p2))

(y2 (car (cdr ,p2))))

(+ (abs (- x1 x2)) (abs (- y1 y2))))

And this is the body of our macro.