TweetFollow Us on Twitter

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))

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:

((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.

((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)
(1 2 3 4 5 6 7 8)
(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))
                     (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))
(EVERY NUMBERP '(1 2 A 4 5))

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)

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))
(FIND-IF (lambda (x) (> x 5)) '(2 3 4 5 6 7 8))

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))
(FIND-IF-NOT  (lambda (x) (> x 5)) '(2 3 4 5 6))

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))

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))

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

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

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))

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)
  (penup) (home) (forward 40)
  (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) 
         (setq speed (read-char))
         (if (numberp speed) (setq tt speed))))
  (if (button) (adjust.controls))
  (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.
(defun adjust.controls (&aux (point (getmouse)))
  (cond (( (car point) (rac point) '(60 -100 80 -60))
         (apply adjust.x point))
        (( (car point) (rac point) '(60 -40 80 0))
         (apply adjust.y point))
        (( (car point) (rac point) '(60 20 80 60))
         (apply adjust.z point))
        (( (car point) (rac point) quit)
         (invertrect quit)
         (wait) (invertrect quit)
         (throw 'flag))))

;* * * * * * * * * * * * * * * * * 
;The three commands ADJUST.X, ADJUST.Z, and ADJUST.Y ;are only
;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.
(defun adjust.x (x y)
  (cond (( x y (car xboxes))
         (invertrect (car xboxes))
         (setq xx (- xx 2))
         (invertrect (car xboxes)))
          (invertrect (rac xboxes))
          (setq xx (+ xx 2))
          (invertrect (rac xboxes)))))

(defun adjust.z (x y)
  (cond (( x y (car zboxes))
         (invertrect (car zboxes))
         (setq zz (- zz 2))
         (invertrect (car zboxes)))
          (invertrect (rac zboxes))
          (setq zz (+ zz 2))
          (invertrect (rac zboxes)))))

(defun adjust.y (x y)
  (cond (( x y (car yboxes))
         (invertrect (car yboxes))
         (setq yy (- yy 2))
         (invertrect (car yboxes)))
          (invertrect (rac yboxes))
          (setq yy (+ yy 2))
          (invertrect (rac yboxes)))))

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

;WAIT waits until the mouse button
; is depressed.Then it returns control
; to the calling function.
(defun wait ()
  (prog ()
        (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 (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:

Software Updates via MacUpdate

Check out the new Pirate Attack update i...
Love pirates and board games? Well, you'll love the new Pirate Attack themed update that just launched in Game of Dice. It adds a bunch of new content themed around pirates, like an all new event map based on a pirate ship which revamps the toll... | Read more »
Splash Cars guide - How to paint the tow...
Splash Cars is an arcade driving game that feels like a hybrid between Dawn of the Plow and Splatoon. In it, you'll need to drive a car around to repaint areas of a town that have lost all of their color. Check out these tips to help you perform... | Read more »
The best video player on mobile
We all know the stock video player on iOS is not particularly convenient, primarily because it asks us to hook a device up to iTunes to sync video in a world that has things like Netflix. [Read more] | Read more »
Four apps to help improve your Super Bow...
Super Bowl Sunday is upon us, and whether you’re a Panthers or a Broncos fan you’re no doubt gearing up for it. [Read more] | Read more »
LooperSonic (Music)
LooperSonic 1.0 Device: iOS Universal Category: Music Price: $4.99, Version: 1.0 (iTunes) Description: LooperSonic is a multi-track audio looper and recorder that will take your loops to the next level. Use it like a loop pedal to... | Read more »
Space Grunts guide - How to survive
Space Grunts is a fast-paced roguelike from popular iOS developer, Orange Pixel. While it taps into many of the typical roguelike sensibilities, you might still find yourself caught out by a few things. We delved further to find you some helpful... | Read more »
Dreii guide - How to play well with othe...
Dreii is a rather stylish and wonderful puzzle game that’s reminiscent of cooperative games like Journey. If that sounds immensely appealing, then you should immediately get cracking and give it a whirl. We can offer you some tips and tricks on... | Read more »
Kill the Plumber World guide - How to ou...
You already know how to hop around like Mario, but do you know how to defeat him? Those are your marching orders in Kill the Plumber, and it's not always as easy as it looks. Here are some tips to get you started. This is not a seasoned platform... | Read more »
Planar Conquest (Games)
Planar Conquest 1.0 Device: iOS Universal Category: Games Price: $12.99, Version: 1.0 (iTunes) Description: IMPORTANT: Planar Conquest is compatible only with iPad 3 & newer devices, iPhone 5 & newer. It’s NOT compatible with... | Read more »
We talk to Cheetah Mobile about its plan...
Piano Tiles 2 is a fast-paced rhythm action high score chaser out now on iOS and Android. You have to tap a series of black tiles that appear on the screen in time to the music, being careful not to accidentally hit anywhere else. Do that and it's... | Read more »

Price Scanner via

BookBook For iPad Pro Coming Soon
The iPad Pro is a device unlike any other, and with Apple Pencil, it’s the ideal portable sketchpad: all that’s missing is the modern easel and portfolio to go. TwelveSouth’s BookBook for iPad Pro... Read more
12-inch 1.2GHz Silver Retina MacBook on sale...
B&H Photo has the 12″ 1.2GHz Silver Retina MacBook on sale for $1399 including free shipping plus NY sales tax only. Their price is $200 off MSRP, and it’s the lowest price for this model from... Read more
iPads on sale at Target: $100 off iPad Air 2,...
Target has WiFi iPad Air 2s and iPad mini 4s on sale for up to $100 off MSRP on their online store for a limited time. Choose free shipping or free local store pickup (if available). Sale prices for... Read more
Target offers Apple Watch for $100 off MSRP
Target has Apple Watches on sale for $100 for a limited time. Choose free shipping or free local store pickup (if available). Sale prices for online orders only, in-store prices may vary: - Apple... Read more
Apple refurbished 2014 13-inch Retina MacBook...
Apple has Certified Refurbished 2014 13″ Retina MacBook Pros available for up to $400 off original MSRP, starting at $979. An Apple one-year warranty is included with each model, and shipping is free... Read more
Macs available for up to $300 off MSRP, $20 o...
Purchase a new Mac or iPad using Apple’s Education Store and take up to $300 off MSRP. All teachers, students, and staff of any educational institution qualify for the discount. Shipping is free, and... Read more
Watch Super Bowl 50 Live On Your iPad For Fre...
Watch Super Bowl 50 LIVE on the CBS Sports app for iPad and Apple TV. Get the app and then tune in Sunday, February 7, 2016 at 6:30 PM ET to catch every moment of the big game. The CBS Sports app is... Read more
Two-thirds Of All Smart Watches Shipped In 20...
Apple dominated the smart watch market in 2015, accounting for over 12 million units and two-thirds of all shipments according to Canalys market research analysts’ estimates. Samsung returned to... Read more
12-inch 1.2GHz Retina MacBooks on sale for up...
B&H Photo has 12″ 1.2GHz Retina MacBooks on sale for $180 off MSRP. Shipping is free, and B&H charges NY tax only: - 12″ 1.2GHz Gray Retina MacBook: $1499 $100 off MSRP - 12″ 1.2GHz Silver... Read more
12-inch 1.1GHz Gray Retina MacBook on sale fo...
B&H Photo has the 12″ 1.1GHz Gray Retina MacBook on sale for $1199 including free shipping plus NY sales tax only. Their price is $100 off MSRP, and it’s the lowest price available for this model... Read more

Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
*Apple* Subject Matter Expert - Experis (Uni...
This position is for an Apple Subject Matter Expert to assist in developing the architecture, support and services for integration of Apple devices into the domain. Read more
*Apple* Macintosh OSX - Net2Source Inc. (Uni...
…: * Work Authorization : * Contact Number(Best time to reach you) : Skills : Apple Macintosh OSX Location : New York, New York. Duartion : 6+ Months The associate would Read more
Computer Operations Technician ll - *Apple*...
# Web Announcement** Apple Technical Liaison**The George Mason University, Information Technology Services (ITS), Technology Support Services, Desktop Support Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.