TweetFollow Us on Twitter

Cons Cells
Volume Number:1
Issue Number:9
Column Tag:Lisp Listener

"Cons Cells and Quickdraw!"

By Andy Cohen, Human Factors Engineer, MacTutor Contributing Editor

The items within a list have pointers to values and to other items. These list inhabitants are refered to as cons cells. A list can be illustrated in terms of it's cons cells. For example, ( A B C D) can be illustrated as follows:

(A B (C D) E) looks like this:

Each pointer for each cell is a stored address of the location in memory of either the value of the cons cell or the next cons cell. It follows then that the cons cell has, two halves. The first half, which points to the value, contains the contents of the address register. While the other half which points to the next cons cell contains the contents of the decrement register. CAR and CDR, remember?. If you are wondering about the similarity between these acronyms and the functions discussed in this column three months ago, you are right, there is a relationship. However, this relationship is not necessarily significant, actually they are analogies. CAR returns the first part of a list or in the context of the cons cell, the value. CDR returns the rest ( or the next cons cell) minus the first. The procedures could just as easily have been called something more meaningful and they can be if one assigned one of these procedures to a preferred name using DEFUN. I am not bringing this subject up as an aid in understanding these lower level functions. My point is to mention that the number of cons cells a machine can handle is a good estimate the capabilities of the machine. With a large "AI" workstation such as the Symbolics 3600 (upon which much of Experlisp was developed) one can expect to have enough memory to utilize in various ways approximately 30,000 cons cells. Using a 512K Macintosh with Experlisp one can obviously expect much less. Added into the latest version of Experlisp (v1.02) there is an undocumented procedure called FREECONS. Contact Expertelligence for an update. Entering FREECONS into the Listener Window returns the number of available cons cells. On a 512K Mac with ExperLisp booted up and with only an untouched ªlispinit file compiled, FREECONS returns 5912. With two megabytes in an XL or a suped up Mac 512 one should expect to get at least 23648 cons cells. Not bad considering the Symbolics 3600 costs in excess of $100,000.

More Predicates

In our discussion on predicates from last month there were a few more predefined predicates provided in Experlisp that were not mentioned. Instead of going through each of them the following table contains most of the predicates listed in the Experlisp reference manual:

Experlisp Predicates:

AND
ARRAYP
ATOM
BOUNDP
BUILTINP
CHARACTERP
CONSP
COPY-LIST
EQUAL
FUNCTIONP
LAMBDAP
LISTP
LOCATIVEP
MACROP
MEMBER
NAND
NEQ
NOT
NULL
NUMBERP
OR
SPECIALP
SYMBOLP
TAILP
TYPE-OF
VARIABLEP
=
>
<

As you may recall a predicate is simply a procedure which tests the value or characteristics of an argument. It always returns what could be thought of as either true or false. Some of the more interesting predicates in the above list include ARRAYP which tests to see if an array was set up and assigned an address, BUILTINP which tests whether or not a function is a valid primitive and returns it's address if it is, and TAILP which tests whether or not a smaller list is at the tail end of a larger list. While these predicates are already defined when Experlisp is opened, there is certainly no reason why one would never want to create their own predicates via DEFUN. For example:

;(DEFUN NAMEP (a)
 (EQUAL "Andy" a))

;(NAMEP "Fred")
;nil
;(NAMEP "Andy")
;t

When defining a predicate the "P" at the tail end of the function name is a typical way to identify it as a predicate. However, predicates are not necessarily always written with this syntax. For example, EQUAL tests for equality between groups of alphanumeric characters (the "=" is used fro numerals).

Con-ditionals

As in most other computer languages there must be a way of performing boolean logic. The terms "If-Then" are typical since they illustrate the logical form. This form is consistent within Experlisp although the syntax is a little different. The COND procedure performs this "If-Then" logic. The typical syntax is as follows:

(COND (("IF" clause) "THEN" values, lists or procedures)

In BASIC one will find many different varieties of "If-Then" commands such as "If-Then-Else", "If-or-then-else" or "If-and- then-else", etc. COND provides us with any variety of conditional statement required. Any appropriate predicate, including one defined by the user, may be used within the clause of the COND. ExperLisp looks through each of these tests or clauses within the COND procedure until one returns anything other then nil. If all the clauses return nil, nil is returned. The following is an example:

(Defun Which (x)
   (cond ((= 1 x) '(the number is one))
          ((= 2 x) '(the number is two))
          ((= 3 x) '(the number is three)))

;Which
;(Which 2)
;the number is two
;(Which 4)
; nil

The above is invoked when the function name and the appropriate form of parameter are evaluated. In the first list inside the COND procedure "(= 1 x)", x is tested for equality with the number one. Since the number two was sent this predicate returns nil. The next list containing "the number is one" is skipped, that is it is not returned and the next line's clause is then evaluated. The parameter, x is then tested for equality with the number two. Since the number two was sent, this list returns "t" and the following list containing " the number is two" is returned in the Listener Window. The third line containing the clause "(= 3 x)" is never evaluated. Once Experlisp gets a nonil from an "IF" clause, the "Then" atoms, lists, or procedures are returned and the COND procedure is completed.

Last month the predicate "MEMBER" was discussed. This predicate, as you may recall, tests to see whether or not an argument is a member of a list. The following sample of COND contains this predicate:

(Setq vegies '(carrots, peas, eggplants, broccoli))
(DEFUN foodfun (x)
   (COND  ((member x vegies) print "it is a member")
           (not (member x vegies) print "It is not a member")))

; (carrots, peas, eggplants, Broccoli)
;foodfun
;(foodfun apple)
;It is not a member
;(foodfun peas)
;it is a member

In the above, the list "(carrots, peas, eggplants, Broccoli)" is assigned as a value to the symbol "vegies". Whenever the term "vegies" is then used (without the single quote, since the single quote will prevent ExperLisp from evaluating the symbol. Symbols have to be evaluated in order to get to their values. Lists or atoms are not evaluated, hence the single quote) it will refer to this list of vegetables. When the defined procedure foodfun is evaluated, along with an appropriate parameter, it passes control to the COND procedure. In this procedure the passed parameter's value is evaluated to see if it is contained within the list represented by the symbol vegies. In the first call to foodfun, the parameter "apple" is not a member of vegie. MEMBER returns nil and control is passed to the next "IF" clause in the COND procedure. This next line is a little more complicated. The parameter is again tested for it's membership within vegies, however there is also an evaluation for the return of a nil or a nonnil. NOT returns "t" for nil and "nil" for a nonnil. Since "apple" is still not a member of vegies the nil is returned. NOT then returns a "t" and then the following atoms, lists or procedures are returned. In this case "It is not a member" is printed in the Listener Window. In the second call to foodfun the parameter value is "peas". This time the first list evaluates it's membership in vegies and returns "t". The arguments, lists or procedures following this evaluation are then invoked. In this second call to "foodfun", "it is a member" is printed.

Note that in the two above samples two different forms of output are used. In "Which" the contents of the list are returned. In "foodfun" the PRINT procedure prints the following text inside the Listener Window. One should also note that the second clause in "foodfun" could be performed just as well with the following:

(t ( print "It is not a member"))

The clause is "t" which forces COND to return "it is not a member". In the above one makes it impossible for COND to return "nil".

ExperLisp provides another simpler form of conditional which can be used instead of COND for situations which don't require multiple clauses. IF acts in the same way as COND except it only has one If-Then-Else sequence. The following demonstrates the IF syntax:

(DEFUN Iftry (x)
 (IF (= 5 x)  '(the number is five) '(the number isn‘t five)))

;(Iftry 2)
;(the number isn‘t five)
;(Iftry 5)
;(the number is five)

IF contains three parts; the "If" condition, the "Then" or true return and the "Else" or false return. Note, that the apostrophe (option "]") was used in the word "isin't". If the single quote was used as an apostrophe by accident the following would result:

;(Iftry 2)
;(the number isn (quote t) five)

ExperLisp reads the single quote as the special symbol used as described above.

Quickdraw!

A language for the Mac without access to the Quickdraw routines in ROM cannot be taken seriously. Obviously, ExperLisp can utilize these routines. They are used with the same syntax as that described in Inside Macintosh. For example the following draws a rectangular figure:

(Paintrect '(top left bottom right))

To draw a circular object or oval try the following:

(Paintoval '(top left bottom right))

Top left and bottom right refers to the coordinates within the specified or default graphics window. These coordinates are as described in last month's column. Top corresponds to the vertical coordinate, left corresponds to the horizontal coordinate. The coordinate order for the bottom points are consistent.

PENPAT is an ExperLisp function which sets the graphic pen output to one of five patterns. The following demonstrates the above Quickdraw routines and the patterns available via PENPAT:

(DEFUN QukDrw ()
   (paintrect '(-52 -56 -32 -32))
   (penpat dkgray)
   (paintoval '(-32 -32 -8 -12))
   (penpat gray)
   (paintrect '(-8 -8 12 16))
   (penpat ltgray)
   (paintoval '(12 16 32 40))
   (penpat black)
   (paintrect '(36 44 52 64))
   (penpat white)
   (paintrect '(38 46 50 62)))

(QukDrw)

The first rectangle uses the default pattern, black. After each object is drawn the pen pattern is changed. The bottom rectangle is actually two. The first is drawn in black, the second is slightly smaller and drawn in white. If drawn by itself the last rectangle would not be visible.

DRAWSTRING puts text into the graphics window. TEXTFONT and TEXTFACE allow the manipulation of the font and style of the text produced from DRAWSTRING, respectively. TEXTSIZE changes the size of the text produced by DRAWSTRING. The following illustrates the syntax of these text control routines in ExperLisp:

(penup) (moveto -82 -35) (pendown)
(textsize 12) (textface (+ 1 8))
(drawstring "Mactutor Magazine")
(penup) (moveto -89 -20) (pendown)
(textfont 1) (textsize 10) (textface (+ 0 4))
(Drawstring "The ONLY Mag On Mac Programming")

Penup and Pendown control when pen movement will and will not produce graphic output. Moveto moves the pen to the given coordinates. The above lists produce the following when compiled:

The text styles are controlled in TEXTFACE using numbers which represent the different styles. The styles and their respective numbers are as follows:

Bold (1) Shadow (16)

Italic (2) Underline (4)

Outline (3) Normal (0)

Text fonts are also represented by numerals. The system font (Chicago) is represented by the number zero. Geneva is 1 and Monaco is 4. Other fonts are supposedly available, but it is not clear how the numbers are assigned. It is likely that the numbers representing the fonts are related to the font's resource ID. This is assigned by the DA/Font Mover just released by Apple.

Next month the Lisp Listener will continue with a discussion on iteration, more on Quickdraw routines and how Mouse input is achieved.

Dr. Tom's Reference Decks

Tom Programs of Washington DC has announced a set of reference cards keyed toInside Macintosh for developers and programmers. Each card lists information about a single toolbox trap call, giving the trap name, address, parameters and notes on how to make use of the trap routine. It provides a handy quick reference during programming that can eliminate the need to flip through the un-referenced Inside Macintosh book. Three decks are being offered, printed on card stock and color coded by manager. Each of the three sets sell for $21.95, a very reasonable price. Alternately, you can buy the entire set as a Microsoft File data base for $59.95, but we think the index card format is much more handy, since your computer is hardly free to run File while your writing code! Contact Tom Programs, Suite 34T, 1500 Massachusetts Ave., NW in Washington, DC. 20005. Or call (223-6813).

One Flew Over The QuickDraw's Nest

Valuable Information Press announces a new technical Macintosh Programming book by Gregg Lewis of Montreal, Canada, titled "One Flew Over the QuickDraw's Nest" (a fan of Jack Nicholson's no doubt!) This book is designed for people who want to program their Macintosh! (Where have I heard that before?) Should be great stuff for MacTutor fans. The book sells for $24.95 and is being distributed by Jim Fitzsimmons at Mac America, the same distributor who handles MacTutor, Macazine and the Macintosh Buyer's Guide. Contact Jim directly to reserve an advance copy. The final manuscript is being prepared for printing now and is expected to be available from Jim at the Mac Expo in Boston at the MacTutor booth. Contact Mac America, (714) 779-2922.

 
AAPL
$94.43
Apple Inc.
+0.49
MSFT
$44.99
Microsoft Corpora
+0.16
GOOG
$597.61
Google Inc.
+8.14

MacTech Search:
Community Search:

Software Updates via MacUpdate

MacBook Air EFI Firmware Update 2.9 - Fo...
MacBook Air EFI Firmware Update is recommended for MacBook Air (Mid 2011) models. This update addresses an issue where systems may take longer to wake from sleep than expected and fixes a rare issue... Read more
FileZilla 3.9.0.1 - Fast and reliable FT...
FileZilla (ported from Windows) is a fast and reliable FTP client and server with lots of useful features and an intuitive interface.Version 3.9.0.1: MSW: Fix installation issue with locked DLLs... Read more
OS X Yosemite 10.10 DP4 - Developer Prev...
Note: This is a Developer Preview. You must be a registered Apple Mac Developer to download this update. OS X Yosemite is Apple's newest operating system for Mac. An elegant design that feels... Read more
FinderPop 2.5.6 - Classic Mac utility, n...
FinderPop is a Universal preference pane that extends OS X's contextual menus using a FinderPop Items folder much as the Apple Menu Items folder used to do for the Apple menu. It has other features... Read more
SpiderOak 5.1.7 - Secure cloud backup, s...
SpiderOak is a multi-platform secure online backup, storage, access, and sharing solution engineered for the consumer and small businesses. You must first sign up to use SpiderOak. Running natively... Read more
Espionage 3.6 - Simple, state of the art...
Espionage offers state-of-the-art encryption and plausible deniability for your confidential data. Sometimes, encrypting your data isn't enough to protect it. That's why Espionage 3 goes beyond data... Read more
calibre 1.45.0 - Complete e-library mana...
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... Read more
iFFmpeg 4.3.1 - Convert multimedia files...
iFFmpeg is a graphical front-end for FFmpeg, a command-line tool used to convert multimedia files between formats. The command line instructions can be very hard to master/understand, so iFFmpeg does... Read more
Chromium 36.0.1985.125 - Fast and stable...
Chromium is an open-source browser project that aims to build a safer, faster, and more stable way for all Internet users to experience the web. FreeSMUG-Free OpenSource Mac User Group build is... Read more
pwSafe 3.0 - Secure password management...
pwSafe provides simple and secure password management across devices and computers. pwSafe uses iCloud to keep your password databases backed-up and synced between Macs and iOS devices. It is... Read more

Latest Forum Discussions

See All

Bio Inc. Review
Bio Inc. Review By Nadia Oxford on July 22nd, 2014 Our Rating: :: SICKENING - IN A COMPELLING WAYUniversal App - Designed for iPhone and iPad Bio Inc is about orchestrating the medical destruction of a single person. If that doesn’... | Read more »
HELMUT Review
HELMUT Review By Andrew Fisher on July 21st, 2014 Our Rating: :: TRUNDLE SIMULATOR 2014Universal App - Designed for iPhone and iPad HELMUT is a fun, fleeting time-sink that offers a momentary distraction and nothing else.   | Read more »
Walkr Review
Walkr Review By Jennifer Allen on July 21st, 2014 Our Rating: :: ORIGINAL WALKINGiPhone App - Designed for the iPhone, compatible with the iPad Walking is a bit more exciting thanks to this planet building/discovering sim reliant... | Read more »
Zombie Commando Review
Zombie Commando Review By Jennifer Allen on July 21st, 2014 Our Rating: :: MINDLESS SLAUGHTERUniversal App - Designed for iPhone and iPad Briefly fun but ultimately forgettable, Zombie Commando will scratch an itch then be... | Read more »
Swords & Poker Adventures Review
Swords & Poker Adventures Review By Jennifer Allen on July 21st, 2014 Our Rating: :: SOULLESS POKER PLAYUniversal App - Designed for iPhone and iPad Swords & Poker Adventures is a mishmash of Poker and RPGing, but it lacks... | Read more »
Warhammer 40,000: The Horus Heresy: Drop...
Warhammer 40,000: The Horus Heresy: Drop Assault Coming Soon to iOS Posted by Jennifer Allen on July 21st, 2014 [ permalink ] Coming soon to iOS will be an all-new Warhammer 40,000 tactical strategy game by the name of The Horus Heresy: Drop As | Read more »
A Life Worth Dying For Review
A Life Worth Dying For Review By Jordan Minor on July 21st, 2014 Our Rating: :: A BEAUTIFUL MINDUniversal App - Designed for iPhone and iPad A Life Worth Dying For is a fascinating portrait of a serious subject.   | Read more »
Zombie Puzzle Panic Review
Zombie Puzzle Panic Review By Jordan Minor on July 21st, 2014 Our Rating: :: THE MATCHING DEADUniversal App - Designed for iPhone and iPad Zombie Puzzle Panic puts some pretty neat undead twists on Match-3 puzzling.   | Read more »
This Week at 148Apps: July 14-18, 2014
Expert App Reviewers   So little time and so very many apps. What’s a poor iPhone/iPad lover to do? Fortunately, 148Apps is here to give you the rundown on the latest and greatest releases. And we even have a tremendous back catalog of reviews; just... | Read more »
Fallen Lords Review
Fallen Lords Review By Andrew Fisher on July 18th, 2014 Our Rating: :: FALLS SHORTiPad Only App - Designed for the iPad Fallen Lords is a decent game, but its similarity and inferiority compared to Ghost Stories makes it ultimately... | Read more »

Price Scanner via MacPrices.net

Twelve South HiRise For MacBook – Height-Adju...
If you use your MacBook as a workhorse desktop substitute, as many of us do, a laptop stand combined with an external keyboard and pointing device are pretty much obligatory if you want to avoid... Read more
Why The Mac Was Not Included In The Apple/IBM...
TUAW’s Yoni Heisler cites Fredrick Paul of Network World whoi blogged last week that the Mac’s conspicuous absence from Apple and IBM’s landmark partnership agreement represents a huge squandered... Read more
Save $100 on 13-inch Retina MacBook Pros, plu...
Adorama has 13″ Retina MacBook Pros on sale for $100 off MSRP. Shipping is free, and Adorama charges sales tax in NY & NJ only: - 13″ 2.4GHz/128GB MacBook Pro with Retina Display: $1199 - 13″ 2.... Read more
Blurr it 2.3 for iOS – Quickly Blurs Selected...
Hyderabad, India based TouchLabs has announced a new update of Blurr it 2.3, their photography app for iOS users. Blurr it allows you to blur part of the image to hide potentially sensitive or... Read more
MacBook Airs on sale for $100 off MSRP, start...
Best Buy has the new 2014 MacBook Airs on sale for up to $100 off MSRP on their online store. Choose free home shipping or free local store pickup (if available). Prices valid for online orders only... Read more
Amazon Announces Kindle Unlimited: Unlimited...
Amazon.com has introduced Kindle Unlimited — a new subscription service which allows customers to freely read as much as they want from over 600,000 Kindle books, and listen as much as they want to... Read more
New Linksys Wireless Range Extenders Boost Wi...
Linksys has announced its new lineup of Linksys Wi-Fi Range Extenders. Consumers often experience a weak wireless signal in some parts of their house or apartment caused by blocking elements such as... Read more
MacBook Airs available starting at $719
The Apple Store has Apple Certified Refurbished 2013 & 2012 MacBook Airs in stock today starting at $719. An Apple one-year warranty is included with each MacBook, and shipping is free: 2013... Read more
Get the best deals on iPad minis with Apple r...
The Apple Store has Certified Refurbished 2nd generation iPad minis with Retina Displays available for up to $130 off the cost of new models, starting at $339. Apple’s one-year warranty is included... Read more
Best Buy’s College Student Deals: $100 off Ma...
Take an additional $100 off all MacBooks and iMacs, $50 off iPad Airs and iPad minis, at Best Buy Online with their College Students Deals Savings, valid through July 25th. Anyone with a valid .EDU... Read more

Jobs Board

*Apple* Computer Technician - Fairfield Coun...
Company DescriptionWe are an Apple Authorized Sales and Service Provider. We have been selling and servicing Apple computers in the Fairfield County area for over 20 Read more
*Apple* Computer Technician - Fairfield Coun...
Company DescriptionWe are an Apple Authorized Sales and Service Provider. We have been selling and servicing Apple computers in the Fairfield County area for over 20 Read more
Mac Expert - *Apple* Online Store Mexico -...
…MUST be fluent in English and Spanish to be considered for this position At Apple , we believe that hard work, a fun environment, creativity and innovation fuel the Read more
*Apple* Computer Technician - Fairfield Coun...
Company DescriptionWe are an Apple Authorized Sales and Service Provider. We have been selling and servicing Apple computers in the Fairfield County area for over 20 Read more
Mac Expert - *Apple* Online Store - Apple (...
**Job Summary** At Apple , we believe that hard work, a fun environment, creativity and innovation fuel the ultimate customer experience. We believe each customer Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.