Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.lisp

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 05-26-2012, 01:01 PM
WJ
Guest
 
Posts: n/a
Default re: lisp newbie here

Alex Mizrahi wrote:

> in kmrcl package you can find such implementations:
>
> (defun alist-plist (alist)
> (apply #'append (mapcar #'(lambda (x) (list (car x) (cdr x))) alist)))


Racket:

(flatten '((k . 2) (key . 3)))
=> '(k 2 key 3)
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 06-11-2012, 09:53 PM
John Thingstad
Guest
 
Posts: n/a
Default Re: lisp newbie here

On 05/26/2012 03:01 PM, WJ wrote:
> Alex Mizrahi wrote:
>
>> in kmrcl package you can find such implementations:
>>
>> (defun alist-plist (alist)
>> (apply #'append (mapcar #'(lambda (x) (list (car x) (cdr x))) alist)))

>
> Racket:
>
> (flatten '((k . 2) (key . 3)))
> => '(k 2 key 3)

CL-USER> (defun alist->plist (alist)
(loop for (car . cdr) in alist
collect car
collect cdr))
ALIST-TO-PLIST
CL-USER> (alist->plist '((1 . 2) (3 . 4) (5 . 6)))
(1 2 3 4 5 6)
CL-USER>

The loop solution seems pretty clean and efficient to me.
This first one is dreadfull as it conses up two cons cells for each
alist element only to delete them afterward. Also it only works if the
alist size is less than half the largest number of arguments to a
function which means it is is not reliable is the alist has more than
500 elements or so.

A flatten that works on improper trees is interesting, however.
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 04:28 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.