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

Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
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 »

Price Scanner via MacPrices.net

Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more

Jobs Board

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
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
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
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.