3-D Rotation
 Volume Number: 1 Issue Number: 13 Column Tag: Lisp Listener

# 3-D Rotations

By Andy Cohen, Engineer, MacTutor Contributing Editor

## Mapping Functions

Applicative operators are functions which use other functions as inputs. One of the most typical across Lisp dialects is MAPCAR. MAPCAR can perform an operation on each member of one given list, sequentially. It is therefore another form of iteration. MAPCAR operates in the same manner APPLY works in the following:

```(apply + '(2 3 4 5))
14
```

MAPCAR, however, provides the capability to perform a given function for each atom within a given list. It can be seen as an "APPLY_TO_ALL". For example, suppose one wanted the square root of each value in a list of five values and create a corresponding list of these square roots. One way to do this is to remove the values from the list with nested CARS. Then, after applying the SQRT function to each value, a new list would need to be produced with a CONS. This method might get needlessly tedious. MAPCAR makes it possible to get this list in a much more convenient fashion.

```(MAPCAR (lambda (x) (SQRT x)) '(2 3 4 5))
(1.41421356 1.73205080 2. 2.23606797)

```

LAMBDA is a special word that tells Lisp that what is following is to be treated as a function similar to DEFUN. LAMBDA acts like a one time only DEFUN. The (x) is the passed value and the list with the square root primitive is the expression to be carried out. MAPCAR takes the first value from the list (2 3 4 5) sequentially (that is why it is MAPCAR) and places it into x. It then puts each computed value into a list. How about another sample:

```(setq x '(2 5)) (setq y '(5 7))
(mapcar (lambda (n) (* .5 n)) (append x y)))
(2  5 )
(5  7 )
(1.  2.5  2.5  3.5 )

```

APPEND puts the values represented by the symbols x and y into a list. MAPCAR takes one atom at a time from the new list and multiplies it by .5.

MAPLIST is another form of mapping or sequencing function. However, instead of sequencing through a list one atom at a time MAPLIST performs a function on two entire lists. For example:

```(MAPLIST APPEND '(A B C) '(D E F))
((A B C D E F)(B C E F)(C F))

```

MAPLIST removes the first atom of both lists and performs the function on the two new lists. It then removes the next two atoms and performs the function on the two lists with the remaining atoms. This continues until there are no more atoms in one of the two lists. The lists do not have to contain the same number of atoms.

```(MAPLIST APPEND '(A B C) '(D E F G H))
((A B C D E F G H)(B C E F G H)
(C F G H))

```

MAPCAN and MAPCON are just like MAPCAR and MAPLIST, respectively except that they do not return lists that are made using LIST. MAPCAN and MAPCON use NCONC. NCONC takes the values from two lists and places all of them into the first, thereby destroying the original list. For example:

```(SETQ x '(1 2 3 4) y '(5 6 7 8))
(NCONC x y)
(5 6 7 8)
(1 2 3 4 5 6 7 8)
x
(1 2 3 4 5 6 7 8)
y
(5 6 7 8)

```

NCONC put all eight values into "x" while it left "y" alone. Since it changed "x" it is considered destructive.An example using MAPCAN follows:

```(MAPCAN (LAMBDA (x) (AND (NUMBERP x)              (SETQ y (SQRT x)) (LIST
y)))'(2 3 4 5 A))
(1.41421356 1.73205080 2. 2.23606797)

```

The AND and the NUMBERP functions in the above, give indication as to when the numbers in the list end by having NUMBERP return nil from the letter "A". AND then stops evaluation. Otherwise the SETQ assigns the square root of each atom to "y" then places it within a list with LIST. Since MAPCAN was used each of the returned values were NCONCed into "y". If one used MAPCAR each value would be placed into a list represented by "y" which would then be placed into the resulting list. The SETQ changes "y" for each value and the resulting list is quite different.

```(MAPCAR (LAMBDA (x) (AND (NUMBERP x)              (SETQ y (SQRT x)) (LIST
y)))'(2 3 4 5 A))
((1.41421356)(1.73205080)(2.)
(2.23606797 ) nil)

```

MAPC and MAPL are also related to MAPCAR and MAPLIST, respectively. These functions are supposed to return the original input list of values instead of a list of resultant values. They are typically used for their side effect such as assigning a new value to a global variable. Unfortunately they don't seem to return results in this manner. Instead of returning the original input list, they both return nil.

EVERY is a totally different type of sequencing function. EVERY applies a predicate to each atom in a list.If the predicate returns "t" for each atom EVERY returns "t". If the predicate returns nil at least once so does EVERY.

```(EVERY NUMBERP '(1 2 3 4 5))
t
(EVERY NUMBERP '(1 2 A 4 5))
nil

```

One interesting feature about EVERY is that one may specify how EVERY will sequence through the list. Without specifying the sequence EVERY defaults to CDR as a step. For example, if we take the second EVERY example above and step through the list with a CDDDR, it returns "t" since the CDDDR makes the evaluation of the predicate skip the letter "A".

```(EVERY NUMBERP '(1 2 A 4 5) cdddr)
t

```

FIND-IF applies a predicate function to each atom in a list until the predicate returns "t". FIND-IF returns the atom which satisfies the predicate then ends the evaluation.

```(FIND-IF EVENP '(3 5 7 8 10))
8
(FIND-IF (lambda (x) (> x 5)) '(2 3 4 5 6 7 8))
6

```

FIND-IF-NOT does the exact opposite of FIND-IF. It returns the first atom of a list which does not satisfy the predicate.

```(FIND-IF EVENP '(2 3 5 7 8))
3
(FIND-IF-NOT  (lambda (x) (> x 5)) '(2 3 4 5 6))
2

```

REDUCE performs a function that requires two inputs. It performs the function on the first two atoms of a list, starting from left to right, then performs the function upon the result of the first two with the very next atom. It then performs the same function with the second result with the next atom. It returns the result when no more atoms are available. For example the following multiplies a sequence of numeric values:

```(REDUCE  * '(7 6 13 76))
41496

```

If one places a T before the last closing parenthesis REDUCE performs the same function on the list from right to left.

```(REDUCE - '(7 6 13 76))
-88

```

The above is equal to (-(-(-7 6)13)76).

``` (REDUCE - '(7 6 13 76) T)
-62

```

The above is now equal to (- 7 (- 6 (- 13 76))).

NOTANY is the equivalent of (NOT (EVERY.... It returns "t" if none of the atoms of a list satisfy the given predicate.

NOTEVERY is the same as FIND-IF-NOT. However, instead of returning the value that did not satisfy the predicate NOTEVERY returns "t" as follows:

```(NOTEVERY  (lambda (x) (> x 5)) '(2 3 4 5 6))
t

```

Besides MAPC and MAPL there is one more mapping function which not only doesnt work as the literature (Common Lisp by Guy Steele) describes it, but just doesnt seem to work in ExperLisp version 1.04. This function is called SOME. SOME is supposed to look successively to the elements of a list and stop when it finds an element which satisfies a predicate function. The following should return the number five:

```(defun try (x)
(oddp x))

(SOME try '(2 4 5))

```

Instead of returning "5" after the defined function name, "unbound variable f" is returned along with a dump of what seems to be the heap.We will be talking to ExperTelligence about the status of these functions.

Last month I mentioned that we would be starting a tutorial on ExperOps5 in this month's issue. Unfortunately, the author of that segment did not make the deadline. We hope to start the tutorial as soon as we can, if it is possible, in coming Mactutor issues. Instead, we are again treated to a nifty program by Dean Ritz of ExperTelligence. The following functions build a small pyramid-like object in three dimensions. Three controls are also produced as seen in Figure 1. These controls rotate the object along three different planes and at varying speeds. After compiling, type (Tetrahedron) into the Listener window and enjoy!

By the way, Happy Holidays!

```;••••••••••••••••••••••••••••••••
;TETRAHEDRON animates a tetrahedron rotating in ;3-Dimensional space.

;It allows a person to adjust the rate of rotation
;through the use of mouse sensative controls.
;Produced by Dean Ritz of ExperTelligence
(defun tetrahedron (&aux (curbun (new3dbun)))
(setq xx 0
yy 0
zz 0
xboxes '((60  -100  80  -80) (60  -80  80  -60))
yboxes '((60  -40  80  -20) (60  -20  80  0))
zboxes '((60  20  80  40) (60  40  80  60))
quit '(60  80  80  120)
std_graf (newgrafwindow '(45 5 310 500)))
(std_graf 'setwtitle "ExperTetrahedron")
(std_graf 'showwindow)
(std_graf 'selectwindow)
(textface 0) (pendown)
(draw.controls)
(penup) (home) (forward 40)
(pendown)
(catch 'flag (doit 1))
(disposhandle (coercetype \$68 curbun))
(std_graf 'closewindow))

(defmacro rac (l)
`(car (last ,l)))

;Hitting a key represent
(defun doit (tt &aux speed)
(cond ((keyp) 
(if (numberp speed) (setq tt speed))))
(roll yy)
(pitch xx)
(yaw zz)
(fillrect '(-130 -100 50 100) white)
(dotimes  (i tt) (tetra 70))
(doit tt))

;* * * * * * * * * * * * * * * * *
;DRAW.CONTROLS draws the mouse sensative controls.

(defun draw.controls ()
(moveto -100 75)
(drawstring "  -   +")
(framerect (car xboxes))
(framerect (rac xboxes))
(moveto -40 75)
(drawstring "  -   +")
(framerect (car yboxes))
(framerect (rac yboxes))
(moveto 20 75)
(drawstring "  -   +")
(framerect (car zboxes))
(framerect (rac zboxes))
(moveto 80 75)
(drawstring "  Quit")
(framerect quit)
(moveto -100 100)
(drawstring " Roll         Pitch        Yaw"))

;* * * * * * * * * * * * * * * * *
;ADJUST.CONTROLS is only called if the mouse button is ;depressed.
;It is responsible for calling the commands which adjust the
;rotation of the tetrahedron.  It also sets the QUIT flag if
;the mouse is clicked in the "Quit" box.
(cond ((pt.in.rect (car point) (rac point) '(60 -100 80 -60))
((pt.in.rect (car point) (rac point) '(60 -40 80 0))
((pt.in.rect (car point) (rac point) '(60 20 80 60))
((pt.in.rect (car point) (rac point) quit)
(invertrect quit)
(wait) (invertrect quit)
(throw 'flag))))

;* * * * * * * * * * * * * * * * *
;called if the mouse is clicked while on one of the controls.
;It inverts the proper button (box), increments a global
;variable for moving the bunny, waits for the mouse button to
;be released, and then re-inverts the button.
(cond ((pt.in.rect x y (car xboxes))
(invertrect (car xboxes))
(setq xx (- xx 2))
(wait)
(invertrect (car xboxes)))
(t
(invertrect (rac xboxes))
(setq xx (+ xx 2))
(wait)
(invertrect (rac xboxes)))))

(cond ((pt.in.rect x y (car zboxes))
(invertrect (car zboxes))
(setq zz (- zz 2))
(wait)
(invertrect (car zboxes)))
(t
(invertrect (rac zboxes))
(setq zz (+ zz 2))
(wait)
(invertrect (rac zboxes)))))

(cond ((pt.in.rect x y (car yboxes))
(invertrect (car yboxes))
(setq yy (- yy 2))
(wait)
(invertrect (car yboxes)))
(t
(invertrect (rac yboxes))
(setq yy (+ yy 2))
(wait)
(invertrect (rac yboxes)))))

;* * * * * * * * * * * * * * * * *

;WAIT waits until the mouse button
; is depressed.Then it returns control
; to the calling function.
(defun wait ()
(prog ()
top
(if (button)
(go top))))

;* * * * * * * * * * * * * * * * *
;TETRA and PART draw a tetrahedron
;using 3-D bunny graphics.
(defun tetra (s)
(dotimes (i 3)
(part s) (roll 90)
(lt -45) (fd s)
(bk s) (lt 45) (roll -90)))

(defun part (s)
(lt 45) (fd s) (rt 45)
(pitch 90)
(rt 45) (fd s) (bk s) (lt 45)
(roll -90))

;* * * * * * * * * * * * * * * * *

;PT.IN.RECT tests to see whether an specific X and Y ;coordiate
;lies within a given boundary rectangle :RECT.
;RECT whould be a list of [TOP LEFT BOTTOM RIGHT] ;coordinates.
(defun pt.in.rect (x y rect)
(and (< x (nth 3 rect))
(  x (nth 1 rect))
(¾ y (nth 2 rect))
(  y (nth 0 rect))
t))  ;returns T if true, NIL otherwise
```

Community Search:
MacTech Search:

Civilization VI 1.1.0 - Next iteration o...
Sid Meier’s Civilization VI is the next entry in the popular Civilization franchise. Originally created by legendary game designer Sid Meier, Civilization is a strategy game in which you attempt to... Read more
Network Radar is an advanced network scanning and managing tool. Featuring an easy-to-use and streamlined design, the all-new Network Radar 2 has been engineered from the ground up as a modern Mac... Read more
Printopia 3.0.8 - Share Mac printers wit...
Run Printopia on your Mac to share its printers to any capable iPhone, iPad, or iPod Touch. Printopia will also add virtual printers, allowing you to save print-outs to your Mac and send to apps.... Read more
ForkLift 3.2.1 - Powerful file manager:...
ForkLift is a powerful file manager and ferociously fast FTP client clothed in a clean and versatile UI that offers the combination of absolute simplicity and raw power expected from a well-executed... Read more
BetterTouchTool 2.417 - Customize multi-...
BetterTouchTool adds many new, fully customizable gestures to the Magic Mouse, Multi-Touch MacBook trackpad, and Magic Trackpad. These gestures are customizable: Magic Mouse: Pinch in / out (zoom... Read more
Little Snitch gives you control over your private outgoing data. Track background activity As soon as your computer connects to the Internet, applications often have permission to send any... Read more
Google Chrome 65.0.3325.181 - Modern and...
Google Chrome is a Web browser by Google, created to be a modern platform for Web pages and applications. It utilizes very fast loading of Web pages and has a V8 engine, which is a custom built... Read more
TeamViewer 13.1.2991 - Establish remote...
TeamViewer gives you remote control of any computer or Mac over the Internet within seconds or can be used for online meetings. Find out why more than 200 million users trust TeamViewer! Free for non... Read more
Printopia 3.0.8 - Share Mac printers wit...
Run Printopia on your Mac to share its printers to any capable iPhone, iPad, or iPod Touch. Printopia will also add virtual printers, allowing you to save print-outs to your Mac and send to apps.... Read more
BetterTouchTool 2.417 - Customize multi-...
BetterTouchTool adds many new, fully customizable gestures to the Magic Mouse, Multi-Touch MacBook trackpad, and Magic Trackpad. These gestures are customizable: Magic Mouse: Pinch in / out (zoom... Read more

## Latest Forum Discussions

The best games that came out for iPhone...
It's not a huge surprise that there's not a massive influx of new, must-buy games on the App Store this week. After all, GDC is happening, so everyone's busy at parties and networking and dying from a sinister form of jetlag. That said, there are... | Read more »
Destiny meets its mobile match - Everyth...
Shadowgun Legends is the latest game in the Shadowgun series, and it's taking the franchise in some interesting new directions. Which is good news. The even better news is that it's coming out tomorrow, so if you didn't make it into the beta you... | Read more »
How PUBG, Fortnite, and the battle royal...
The history of the battle royale genre isn't a long one. While the nascent parts of the experience have existed ever since players first started killing one another online, it's really only in the past six years that the genre has coalesced into... | Read more »
Around the Empire: What have you missed...
Oh hi nice reader, and thanks for popping in to check out our weekly round-up of all the stuff that you might have missed across the Steel Media network. Yeah, that's right, it's a big ol' network. Obviously 148Apps is the best, but there are some... | Read more »
All the best games on sale for iPhone an...
It might not have been the greatest week for new releases on the App Store, but don't let that get you down, because there are some truly incredible games on sale for iPhone and iPad right now. Seriously, you could buy anything on this list and I... | Read more »
Everything You Need to Know About The Fo...
In just over a week, Epic Games has made a flurry of announcements. First, they revealed that Fortnite—their ultra-popular PUBG competitor—is coming to mobile. This was followed by brief sign-up period for interested beta testers before sending out... | Read more »
The best games that came out for iPhone...
It's not been the best week for games on the App Store. There are a few decent ones here and there, but nothing that's really going to make you throw down what you're doing and run to the nearest WiFi hotspot in order to download it. That's not to... | Read more »
Death Coming (Games)
Death Coming 1.1.1.536 Device: iOS Universal Category: Games Price: \$1.99, Version: 1.1.1.536 (iTunes) Description: --- Background Story ---You Died. Pure and simple, but death was not the end. You have become an agent of Death: a... | Read more »
Hints, tips, and tricks for Empires and...
Empires and Puzzles is a slick match-stuff RPG that mixes in a bunch of city-building aspects to keep things fresh. And it's currently the Game of the Day over on the App Store. So, if you're picking it up for the first time today, we thought it'd... | Read more »
What You Need to Know About Sam Barlow’s...
Sam Barlow’s follow up to Her Story is #WarGames, an interactive video series that reimagines the 1983 film WarGames in a more present day context. It’s not exactly a game, but it’s definitely still interesting. Here are the top things you should... | Read more »

## Price Scanner via MacPrices.net

Thursday roundup of the best 13″ MacBook Pro...
B&H Photo has new 2017 13″ MacBook Pros on sale for up to \$200 off MSRP. Shipping is free, and B&H charges sales tax for NY & NJ residents only. Their prices are the lowest available for... Read more
Sale: 9.7-inch 2017 WiFi iPads starting at \$2...
B&H Photo has 9.7″ 2017 WiFi Apple iPads on sale for \$40 off MSRP for a limited time. Shipping is free, and pay sales tax in NY & NJ only: – 32GB iPad WiFi: \$289, \$40 off – 128GB iPad WiFi: \$... Read more
Apple has Certified Refurbished 9.7″ WiFi iPads available for \$50-\$80 off the cost of new models. An Apple one-year warranty is included with each iPad, and shipping is free: – 9″ 32GB WiFi iPad: \$... Read more
Back in stock! Apple’s full line of Certified...
Save \$300-\$300 on the purchase of a 2017 13″ MacBook Pro today with Certified Refurbished models at Apple. Apple’s refurbished prices are the lowest available for each model from any reseller. A... Read more
Wednesday deals: Huge sale on Apple 15″ MacBo...
Adorama has new 2017 15″ MacBook Pros on sale for \$250-\$300 off MSRP. Shipping is free, and Adorama charges sales tax in NJ and NY only: – 15″ 2.8GHz Touch Bar MacBook Pro Space Gray (MPTR2LL/A): \$... Read more
Apple offers Certified Refurbished Series 3 A...
Apple has Certified Refurbished Series 3 Apple Watch GPS models available for \$50, or 13%, off the cost of new models. Apple’s standard 1-year warranty is included, and shipping is free. Numerous... Read more
12″ 1.2GHz Space Gray MacBook on sale for \$11...
B&H Photo has the Space Gray 12″ 1.2GHz MacBook on sale for \$100 off MSRP. Shipping is free, and B&H charges sales tax for NY & NJ residents only: – 12″ 1.2GHz Space Gray MacBook: \$1199 \$... Read more
Mac minis available for up to \$150 off MSRP w...
Apple has restocked Certified Refurbished Mac minis starting at \$419. Apple’s one-year warranty is included with each mini, and shipping is free: – 1.4GHz Mac mini: \$419 \$80 off MSRP – 2.6GHz Mac... Read more
Back in stock: 13-inch 2.5GHz MacBook Pro (Ce...
Apple has Certified Refurbished 13″ 2.5GHz MacBook Pros (MD101LL/A) available for \$829, or \$270 off original MSRP. Apple’s one-year warranty is standard, and shipping is free: – 13″ 2.5GHz MacBook... Read more
Apple restocks Certified Refurbished 2017 13″...
Apple has Certified Refurbished 2017 13″ MacBook Airs available starting at \$849. An Apple one-year warranty is included with each MacBook, and shipping is free: – 13″ 1.8GHz/8GB/128GB MacBook Air (... Read more

## Jobs Board

*Apple* Genius - Technical Customer Service...
Job Description:Job SummaryAs a Genius at the Apple Store, you maintain customers' trust in Apple as the skilled technical customer service expert, Read more