Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion I would appreciate a code review
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Kenny Tilton  
View profile  
 More options Nov 7 2003, 11:57 am
Newsgroups: comp.lang.lisp
From: Kenny Tilton <ktil...@nyc.rr.com>
Date: Fri, 07 Nov 2003 16:51:21 GMT
Local: Fri, Nov 7 2003 11:51 am
Subject: Re: I would appreciate a code review

Artie Gold wrote:
> (defun build-occurences-alist (the-list)
>    ; the-alist is the result
>    (let ((the-alist nil))
>      (flet ((update-alist (key)
>        ; have we dealt with this symbol already?
>        (let ((pos (assoc key the-alist)))
>          (if pos
>            ; we have, so increment the count
>            (incf (cdr pos))
>            ; we haven't, so create an entry
>            (push (cons key 1) the-alist)))))
>        ; map across the supplied list
>        (mapc #'update-alist the-list)
>        ; I can't get the sort to work for some reason
>        ;(sort the-alist #'> :key #'cdr)
>        the-alist)))

> Aside from the fact that I can't seem to get the SORT to work (any clues
> would, of course, be welcome)

This is an FAQ classic: you have to capture the return value from sort.
Remember, there is no such thing as a list. The local variable the-alist
is bound to a cons cell (the first of the list before the sort). after
the sort (since you do not code "(setf the-alist (sort the-alist....")
it naturally is still bound to the same cons cell, no matter where that
cons cell now sits in the structure which followed it until sort
destructively rearranged things.

Of course in the example above it so happens you do not need the "(setf
the-alist (sort..." at all since you could just return what sort returns
(the new first cons cell).

kenny

--
http://tilton-technology.com

Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film

Your Project Here! http://alu.cliki.net/Industry%20Application


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.