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.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »
Explore some of BBCs' most iconic s...
Despite your personal opinion on the BBC at a managerial level, it is undeniable that it has overseen some fantastic British shows in the past, and now thanks to a partnership with Roblox, players will be able to interact with some of these... | Read more »
Play Together teams up with Sanrio to br...
I was quite surprised to learn that the massive social network game Play Together had never collaborated with the globally popular Sanrio IP, it seems like the perfect team. Well, this glaring omission has now been rectified, as that instantly... | Read more »
Dark and Darker Mobile gets a new teaser...
Bluehole Studio and KRAFTON have released a new teaser trailer for their upcoming loot extravaganza Dark and Darker Mobile. Alongside this look into the underside of treasure hunting, we have received a few pieces of information about gameplay... | Read more »

Price Scanner via MacPrices.net

14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more
Sunday Sale: 13-inch M3 MacBook Air for $999,...
Several Apple retailers have the new 13″ MacBook Air with an M3 CPU in stock and on sale today for only $999 in Midnight. These are the lowest prices currently available for new 13″ M3 MacBook Airs... Read more
Multiple Apple retailers are offering 13-inch...
Several Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices... Read more
Roundup of Verizon’s April Apple iPhone Promo...
Verizon is offering a number of iPhone deals for the month of April. Switch, and open a new of service, and you can qualify for a free iPhone 15 or heavy monthly discounts on other models: – 128GB... Read more
B&H has 16-inch MacBook Pros on sale for...
Apple 16″ MacBook Pros with M3 Pro and M3 Max CPUs are in stock and on sale today for $200-$300 off MSRP at B&H Photo. Their prices are among the lowest currently available for these models. B... Read more
Updated Mac Desktop Price Trackers
Our Apple award-winning Mac desktop price trackers are the best place to look for the lowest prices and latest sales on all the latest computers. Scan our price trackers for the latest information on... Read more
9th-generation iPads on sale for $80 off MSRP...
Best Buy has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80 off MSRP on their online store for a limited time. Prices start at only $249. Sale prices for online orders only, in-store prices... Read more
15-inch M3 MacBook Airs on sale for $100 off...
Best Buy has Apple 15″ MacBook Airs with M3 CPUs on sale for $100 off MSRP on their online store. Prices valid for online orders only, in-store prices may vary. Order online and choose free shipping... Read more

Jobs Board

Sublease Associate Optometrist- *Apple* Val...
Sublease Associate Optometrist- Apple Valley, CA- Target Optical Date: Mar 22, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92307 **Requisition Read more
Early Preschool Teacher - Glenda Drive/ *Appl...
Early Preschool Teacher - Glenda Drive/ Apple ValleyTeacher Share by Email Share on LinkedIn Share on Twitter Read more
Retail Assistant Manager- *Apple* Blossom Ma...
Retail Assistant Manager- APPLE BLOSSOM MALL Brand: Bath & Body Works Location: Winchester, VA, US Location Type: On-site Job ID: 04225 Job Area: Store: Management Read more
Housekeeper, *Apple* Valley Village - Cassi...
Apple Valley Village Health Care Center, a senior care campus, is hiring a Part-Time Housekeeper to join our team! We will train you for this position! In this role, Read more
Sonographer - *Apple* Hill Imaging Center -...
Sonographer - Apple Hill Imaging Center - Evenings Location: York Hospital, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now See Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.