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))
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) 
         (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 ((pt.in.rect (car point) (rac point) '(60 -100 80 -60))
         (apply adjust.x point))
        ((pt.in.rect (car point) (rac point) '(60 -40 80 0))
         (apply adjust.y point))
        ((pt.in.rect (car point) (rac point) '(60 20 80 60))
         (apply adjust.z point))
        ((pt.in.rect (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 ((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)))))

(defun adjust.z (x y)
  (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)))))

(defun adjust.y (x y)
  (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:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »
Live, Playdate, Live! – The TouchArcade...
In this week’s episode of The TouchArcade Show we kick things off by talking about all the games I splurged on during the recent Playdate Catalog one-year anniversary sale, including the new Lucas Pope jam Mars After Midnight. We haven’t played any... | Read more »

Price Scanner via MacPrices.net

Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more
New promo at Visible: Buy a new iPhone, get $...
Switch to Visible, and buy a new iPhone, and Visible will take $10 off their monthly Visible+ service for 24 months. Visible+ is normally $45 per month. With this promotion, the cost of Visible+ is... Read more
B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for $100 off Apple’s new MSRP, only $899. Free 1-2 day delivery is available to most US addresses. Their... Read more
Take advantage of Apple’s steep discounts on...
Apple has a full line of 16″ M3 Pro and M3 Max MacBook Pros available, Certified Refurbished, starting at $2119 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.