Google Groups Home
Help | Sign in
Message from discussion Beginner question
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
Andreas Thiele  
View profile
 More options Jan 31 2005, 5:42 am
Newsgroups: comp.lang.lisp
From: "Andreas Thiele" <nospam6...@nospam.com>
Date: Mon, 31 Jan 2005 11:42:34 +0100
Local: Mon, Jan 31 2005 5:42 am
Subject: Re: Beginner question
"Yoel Jacobsen" <y...@emet.co.il> schrieb im Newsbeitrag
news:ctkt7f$ipm$1@news2.netvision.net.il...

> I'm experimenting CL by attempting to write some basic programs.

> 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)))

Another more verbose iterative solution might be easier to read:

(defun pline-to-plist2 (x)
  (let ((r '()))
    (mapc #'(lambda (key value)
              (push key r)
              (push value r))
          '(:login :passwd :uid :gid :group :home :shell)
          (split-sequence:split-sequence #\: x))
    (nreverse r)))

Andreas


    Reply to author    Forward  
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.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google