[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Beginner question

William James

8/28/2015 2:41:00 PM

Andreas Thiele wrote:

> > I'm trying to write an /etc/passwd to LDIF converter and I'm stuck - I
> > can't figure out from CLISP errors what is wrong. Here is my code:
> >
> > (require 'split-sequence)
> >
> > (defmacro list-to-plist (plist pos-list id-list field-list)
> > `(progn ,@(map 'list
> > #'(lambda (id pos)
> > `(setf (getf ,id ,id-list) (elt ,field-list ,pos)))
> > plist pos-list)))
> >
> > (defun pline-to-plist (line)
> > "convert a passwd line to a plist"
> > (let ((fields (split-sequence:split-sequence #\: line))
> > (entry nil))
> > (list-to-plist
> > '(:login :passwd :uid :gid :group :home :shell)
> > '(0 1 2 3 4 5 6 7)
> > entry fields)
> > entry))
> >
> > (format t "~S~%" (pline-to-plist
> > "news:x:9:13:news:/usr/lib/news:/bin/false"))
> >
> > I tried to use a macro to shorten the following pattern:
> > (SETF (GETF :LOGIN ENTRY) (ELT FIELDS 0))
> > (SETF (GETF :PASSWD ENTRY) (ELT FIELDS 1))
> > (SETF (GETF :UID ENTRY) (ELT FIELDS 2))
> > (SETF (GETF :GID ENTRY) (ELT FIELDS 3))
> > (SETF (GETF :GROUP ENTRY) (ELT FIELDS 4))
> > (SETF (GETF :HOME ENTRY) (ELT FIELDS 5))
> > (SETF (GETF :SHELL ENTRY) (ELT FIELDS 6))
> >
> > Now, I get diffetent errors:
> > Inside SLIME -
> > FUNCALL: undefined function NIL
> > [Condition of type SYSTEM::SIMPLE-UNDEFINED-FUNCTION]
> > When evaluating directly with CLISP:
> > *** - SYSTEM::%EXPAND-FORM: invalid form (0 1 2 3 4 5 6 7)
> >
> > Where did I wrong?
>
> Yoel,
>
> you should use (macroexpand '(list-to-plist ...)) to see what your macro
> really creates.
>
> Let me suggest a typical lisp idiom to solve your problem:
>
> (defun pline-to-plist (x)
> (mapcan #'(lambda (key value) (list key value))
> '(:login :passwd :uid :gid :group :home :shell)
> (split-sequence:split-sequence #\: x)))

Testing:

CL-USER(1): (split-sequence:split-sequence #\: "foo:bar")

Debugger invoked on condition of type PACKAGE-ERROR:
Package "SPLIT-SEQUENCE" not found.


Gauche Scheme:

(define (pline-to-alist pline)
(map
cons
'(:login :passwd :uid :gid :group :home :shell)
(string-split pline ":")))

(pline-to-alist "news:x:9:13:news:/usr/lib/news:/bin/false")
===>
((:login . "news") (:passwd . "x") (:uid . "9") (:gid . "13")
(:group . "news") (:home . "/usr/lib/news") (:shell . "/bin/false"))

--
The report card by the American Society of Civil Engineers showed the national
infrastructure a single grade above failure, a step from declining to the point
where everyday things simply stop working the way people expect them to. ---
washingtonpost.com/local/trafficandcommuting/us-infrastructure-gets-d-in-annual-report/2013/03/19/c48cb010-900b-11e2-9cfd-36d6c9b5d7ad_story.html