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

calibre 2.17 - Complete e-library manage...
Calibre is a complete e-book library manager. Organize your collection, convert your books to multiple formats, and sync with all of your devices. Let Calibre be your multi-tasking digital librarian... Read more
OmniGraffle Pro 6.1.2 - Create diagrams,...
OmniGraffle Pro helps you draw beautiful diagrams, family trees, flow charts, org charts, layouts, and (mathematically speaking) any other directed or non-directed graphs. We've had people use... Read more
OmniGraffle 6.1.2 - Create diagrams, flo...
OmniGraffle helps you draw beautiful diagrams, family trees, flow charts, org charts, layouts, and (mathematically speaking) any other directed or non-directed graphs. We've had people use Graffle to... Read more
RoboForm 2.0.2 - Password manager; syncs...
RoboForm is a password manager that offers one-click login, mobile syncing, easy form filling, and reliable security. Password Manager. RoboForm remembers your passwords so you don't have to! Just... Read more
Apple MainStage 3.1 - Live performance t...
Love the sound you got on your recording? MainStage 3 makes it easy to bring all the same instruments and effects to the stage. Everything from the Sound Library and Smart Controls you're familiar... Read more
Freeway Pro 7.0.2 - Drag-and-drop Web de...
Freeway Pro lets you build websites with speed and precision... without writing a line of code! With its user-oriented drag-and-drop interface, Freeway Pro helps you piece together the website of... Read more
A Better Finder Rename 9.44 - File, phot...
A Better Finder Rename is the most complete renaming solution available on the market today. That's why, since 1996, tens of thousands of hobbyists, professionals and businesses depend on A Better... Read more
Stacks 2.6.9 - New way to create pages i...
Stacks is a new way to create pages in RapidWeaver. It's a plugin designed to combine drag-and-drop simplicity with the power of fluid layout. Features: Fluid Layout: Stacks lets you build pages... Read more
Sid Meier's Civilization: Beyond Ea...
Sid Meier's Civilization: Beyond Earth is a new science-fiction-themed entry into the award-winning Civilization series. Set in the future, global events have destabilized the world leading to a... Read more
Logic Pro X 10.1 - Music creation and au...
Apple Logic Pro X is the most advanced version of Logic ever. Sophisticated new tools for professional songwriting, editing, and mixing are built around a modern interface that's designed to get... Read more

Choice Provisions is Set to Launch Destr...
Choice Provisions is Set to Launch Destructamundo on iOS This Month Posted by Tre Lawrence on January 23rd, 2015 [ permalink ] Choice Provisions – home stable to | Read more »
King of Thieves – An Interview With Zept...
Ahead of the release of ZeptoLab’s King of Thieves, we were able to ask ZeptoLab’s co-founder, Semyon Voinov, a few questions about the inspiration behind the game and what that means for the Cut the Rope franchise. | Read more »
Handle Review
Handle Review By Jennifer Allen on January 23rd, 2015 Our Rating: :: SPEEDY ORGANIZINGUniversal App - Designed for iPhone and iPad Handle is a very convenient way of juggling your emails, To Do list, and Calendar all through one... | Read more »
The New Disney Inquizitive App Offers a...
The New Disney Inquizitive App Offers a Place for Fans to Take Disney Quizzes Posted by Tre Lawrence on January 23rd, 2015 [ permalink ] | Read more »
Hands-On With Cut the Rope Developer Zep...
Marking quite a departure from ZeptoLab’s past successes, namely the Cut The Rope series, King of Thieves is shaping up to be quite promising. Due for release in February, we were lucky enough to have some time with a preview build to see exactly... | Read more »
Fast Fishing Review
Fast Fishing Review By Jennifer Allen on January 23rd, 2015 Our Rating: :: LIVES UP TO ITS NAMEUniversal App - Designed for iPhone and iPad Fishing is far from relaxing in Fast Fishing, but it is fun.   | Read more »
The LEGO Movie Video Game is Available N...
The LEGO Movie Video Game is Available Now for iOS Posted by Ellis Spice on January 23rd, 2015 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Satellina Review
Satellina Review By Jennifer Allen on January 23rd, 2015 Our Rating: :: TWITCHY BUT TACTICALUniversal App - Designed for iPhone and iPad Satellina requires quick thinking and twitchy fingers, and it’s pretty fun.   | Read more »
Tail Drift, the Crazy 360 Degree Flyer,...
Tail Drift, the Crazy 360 Degree Flyer, Has Gone Free-to-Play in a New Update Posted by Jessica Fisher on January 22nd, 2015 [ permalink ] | Read more »
PureSkate 2 Review
PureSkate 2 Review By Tre Lawrence on January 22nd, 2015 Our Rating: :: ALMOST ALL AIRUniversal App - Designed for iPhone and iPad PureSkate 2 lets one’s fingers do the skateboarding.   | Read more »

Price Scanner via

College Student Deals are back, additional $5...
Take an additional $50 off all MacBooks and iMacs at Best Buy Online with their College Students Deals Savings, valid through April 11, 2015. Anyone with a valid .EDU email address can take advantage... Read more
iPhone 6 and 6 Plus GIve Apple Half Of US Mob...
Chicago-based Consumer Intelligence Research Partners, LLC (CIRP) have released analysis of the results of its research on mobile phone manufacturers for the calendar quarter that ended December 31,... Read more
Save $100 on MacBook Airs with 256GB of stora...
B&H Photo has 256GB MacBook Airs on sale for $100 off MSRP. Shipping is free, and B&H charges NY sales tax only: - 11″ 1.4GHz/256GB MacBook Air: $999 $100 off MSRP - 13″ 1.4GHz/256GB MacBook... Read more
21-inch 2.7GHz iMac on sale for $1179, save $...
B&H Photo has the 21″ 2.7GHz iMac on sale for $1179 including free shipping plus NY sales tax only. Their price is $120 off MSRP, and it’s the lowest price available for this model from any... Read more
iPhone Usage Rates by State Correlate With Ed...
Chitika Insights notes that despite iPhones being the largest source of smartphone Internet traffic in North America, their latest study finds a relatively high degree of variation of iPhone usage... Read more
ProGearX Extendable Pole “Pov/Selfie Stick” M...
There’s something inescapably narcissistic about the concept of selfies as they’ve developed as a smartphone-driven social (particularly social media) phenomenon that rubs me the wrong way. However,... Read more
iPad Air 2 on sale for up to $100 off MSRP, 2...
 Best Buy has iPad Air 2s on sale for up to $100 off MSRP on their online store for the next two days. Choose free shipping or free local store pickup (if available). Sale prices available for online... Read more
Roundup of Apple refurbished MacBook Pros and...
The Apple Store has Apple Certified Refurbished 2014 MacBook Pros and MacBook Airs available for up to $400 off the cost of new models. An Apple one-year warranty is included with each model, and... Read more
Sale! 13-inch 2.8GHz Retina MacBook Pro for $...
 B&H Photo has the 13″ 2.8GHz Retina MacBook Pro on sale for $1599 including free shipping plus NY sales tax only. Their price is $200 off MSRP, and it’s the lowest price available for this model... Read more
Next OS X/iOS Version Upgrades Should Concent...
On stage at Apple’s World Wide Developers’ Conference in June 2009, Bertrand Serlet, the company’s Senior Vice President of Software Engineering at the time, announced that the forthcoming OS X... Read more

Jobs Board

*Apple* Solutions Consultant (ASC)- Retail S...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
*Apple* Solutions Consultant - Retail Sales...
**Job Summary** As an Apple Solutions Consultant (ASC) you are the link between our customers and our products. Your role is to drive the Apple business in a retail Read more
*Apple* Lead Operator, GSOC - Apple (United...
**Job Summary** Apple is seeking an exceptional, customer service oriented and experienced persons to fulfill the role of Apple Lead Operator (ALO) as part of the Read more
Order Support Supervisor- *Apple* Online Sto...
**Job Summary** The Apple Online Store (AOS) Order Administration team is looking for an Order Support Supervisor to manage and lead a team of Specialists through the Read more
Senior Program Manager, *Apple* Online Supp...
**Job Summary** The Apple Online Support Planning team is looking for an experienced Senior Project Manager to lead key Quality program initiatives across the Online Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.