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

Ableton Live 11.3.11 - Record music usin...
Ableton Live lets you create and record music on your Mac. Use digital instruments, pre-recorded sounds, and sampled loops to arrange, produce, and perform your music like never before. Ableton Live... Read more
Affinity Photo 2.2.0 - Digital editing f...
Affinity Photo - redefines the boundaries for professional photo editing software for the Mac. With a meticulous focus on workflow it offers sophisticated tools for enhancing, editing and retouching... Read more
SpamSieve 3.0 - Robust spam filter for m...
SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering. SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more
WhatsApp 2.2338.12 - Desktop client for...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more
Fantastical 3.8.2 - Create calendar even...
Fantastical is the Mac calendar you'll actually enjoy using. Creating an event with Fantastical is quick, easy, and fun: Open Fantastical with a single click or keystroke Type in your event details... Read more
iShowU Instant 1.4.14 - Full-featured sc...
iShowU Instant gives you real-time screen recording like you've never seen before! It is the fastest, most feature-filled real-time screen capture tool from shinywhitebox yet. All of the features you... Read more
Geekbench 6.2.0 - Measure processor and...
Geekbench provides a comprehensive set of benchmarks engineered to quickly and accurately measure processor and memory performance. Designed to make benchmarks easy to run and easy to understand,... Read more
Quicken 7.2.3 - Complete personal financ...
Quicken makes managing your money easier than ever. Whether paying bills, upgrading from Windows, enjoying more reliable downloads, or getting expert product help, Quicken's new and improved features... Read more
EtreCheckPro 6.8.2 - For troubleshooting...
EtreCheck is an app that displays the important details of your system configuration and allow you to copy that information to the Clipboard. It is meant to be used with Apple Support Communities to... Read more
iMazing 2.17.7 - Complete iOS device man...
iMazing is the world’s favourite iOS device manager for Mac and PC. Millions of users every year leverage its powerful capabilities to make the most of their personal or business iPhone and iPad.... Read more

Latest Forum Discussions

See All

‘Junkworld’ Is Out Now As This Week’s Ne...
Epic post-apocalyptic tower-defense experience Junkworld () from Ironhide Games is out now on Apple Arcade worldwide. We’ve been covering it for a while now, and even through its soft launches before, but it has returned as an Apple Arcade... | Read more »
Motorsport legends NASCAR announce an up...
NASCAR often gets a bad reputation outside of America, but there is a certain charm to it with its close side-by-side action and its focus on pure speed, but it never managed to really massively break out internationally. Now, there's a chance... | Read more »
Skullgirls Mobile Version 6.0 Update Rel...
I’ve been covering Marie’s upcoming release from Hidden Variable in Skullgirls Mobile (Free) for a while now across the announcement, gameplay | Read more »
Amanita Design Is Hosting a 20th Anniver...
Amanita Design is celebrating its 20th anniversary (wow I’m old!) with a massive discount across its catalogue on iOS, Android, and Steam for two weeks. The announcement mentions up to 85% off on the games, and it looks like the mobile games that... | Read more »
SwitchArcade Round-Up: ‘Operation Wolf R...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for September 21st, 2023. I got back from the Tokyo Game Show at 8 PM, got to the office here at 9:30 PM, and it is presently 11:30 PM. I’ve done what I can today, and I hope you enjoy... | Read more »
Massive “Dark Rebirth” Update Launches f...
It’s been a couple of months since we last checked in on Diablo Immortal and in that time the game has been doing what it’s been doing since its release in June of last year: Bringing out new seasons with new content and features. | Read more »
‘Samba De Amigo Party-To-Go’ Apple Arcad...
SEGA recently released Samba de Amigo: Party-To-Go () on Apple Arcade and Samba de Amigo: Party Central on Nintendo Switch worldwide as the first new entries in the series in ages. | Read more »
The “Clan of the Eagle” DLC Now Availabl...
Following the last paid DLC and free updates for the game, Playdigious just released a new DLC pack for Northgard ($5.99) on mobile. Today’s new DLC is the “Clan of the Eagle" pack that is available on both iOS and Android for $2.99. | Read more »
Let fly the birds of war as a new Clan d...
Name the most Norse bird you can think of, then give it a twist because Playdigious is introducing not the Raven clan, mostly because they already exist, but the Clan of the Eagle in Northgard’s latest DLC. If you find gathering resources a... | Read more »
Out Now: ‘Ghost Detective’, ‘Thunder Ray...
Each and every day new mobile games are hitting the App Store, and so each week we put together a big old list of all the best new releases of the past seven days. Back in the day the App Store would showcase the same games for a week, and then... | Read more »

Price Scanner via MacPrices.net

Apple AirPods 2 with USB-C now in stock and o...
Amazon has Apple’s 2023 AirPods Pro with USB-C now in stock and on sale for $199.99 including free shipping. Their price is $50 off MSRP, and it’s currently the lowest price available for new AirPods... Read more
New low prices: Apple’s 15″ M2 MacBook Airs w...
Amazon has 15″ MacBook Airs with M2 CPUs and 512GB of storage in stock and on sale for $1249 shipped. That’s $250 off Apple’s MSRP, and it’s the lowest price available for these M2-powered MacBook... Read more
New low price: Clearance 16″ Apple MacBook Pr...
B&H Photo has clearance 16″ M1 Max MacBook Pros, 10-core CPU/32-core GPU/1TB SSD/Space Gray or Silver, in stock today for $2399 including free 1-2 day delivery to most US addresses. Their price... Read more
Switch to Red Pocket Mobile and get a new iPh...
Red Pocket Mobile has new Apple iPhone 15 and 15 Pro models on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide service using all the major... Read more
Apple continues to offer a $350 discount on 2...
Apple has Studio Display models available in their Certified Refurbished store for up to $350 off MSRP. Each display comes with Apple’s one-year warranty, with new glass and a case, and ships free.... Read more
Apple’s 16-inch MacBook Pros with M2 Pro CPUs...
Amazon is offering a $250 discount on new Apple 16-inch M2 Pro MacBook Pros for a limited time. Their prices are currently the lowest available for these models from any Apple retailer: – 16″ MacBook... Read more
Closeout Sale: Apple Watch Ultra with Green A...
Adorama haș the Apple Watch Ultra with a Green Alpine Loop on clearance sale for $699 including free shipping. Their price is $100 off original MSRP, and it’s the lowest price we’ve seen for an Apple... Read more
Use this promo code at Verizon to take $150 o...
Verizon is offering a $150 discount on cellular-capable Apple Watch Series 9 and Ultra 2 models for a limited time. Use code WATCH150 at checkout to take advantage of this offer. The fine print: “Up... Read more
New low price: Apple’s 10th generation iPads...
B&H Photo has the 10th generation 64GB WiFi iPad (Blue and Silver colors) in stock and on sale for $379 for a limited time. B&H’s price is $70 off Apple’s MSRP, and it’s the lowest price... Read more
14″ M1 Pro MacBook Pros still available at Ap...
Apple continues to stock Certified Refurbished standard-configuration 14″ MacBook Pros with M1 Pro CPUs for as much as $570 off original MSRP, with models available starting at $1539. Each model... 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
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
Retail Key Holder- *Apple* Blossom Mall - Ba...
Retail Key Holder- APPLE BLOSSOM MALL Brand: Bath & Body Works Location: Winchester, VA, US Location Type: On-site Job ID: 03YM1 Job Area: Store: Sales and Support 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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.