From: Drew Krause
Subject: reporting on the "define"d
Date: 
Message-ID: <13b19kafl8pg92@corp.supernews.com>
..so I have a list of 'defined' lists & would like to report on them. 
I'm confused as to how I might mix the name of each variable with its 
information.

Here's an example:

(define fred (list 3 4 5))
(define george (list 1 2 3 4 5 6))

(define mylist (list fred george))

How to write a function that returns name + list length, e.g.

(greatfunc mylist) => ((fred 3) (george 5))

many thanks!

From: Thomas A. Russ
Subject: Re: reporting on the "define"d
Date: 
Message-ID: <ymivebzrtfu.fsf@blackcat.isi.edu>
Drew Krause <········@mindspring.com> writes:

> ..so I have a list of 'defined' lists & would like to report on
> them. I'm confused as to how I might mix the name of each variable with
> its information.
> 
> Here's an example:
> 
> (define fred (list 3 4 5))
> (define george (list 1 2 3 4 5 6))
> 
> (define mylist (list fred george))
> 
> How to write a function that returns name + list length, e.g.
> 
> (greatfunc mylist) => ((fred 3) (george 5))
> 
> many thanks!

As presented here, there is no way to retrieve the names FRED and GEORGE
from the value of MYLIST.

That is because, when you write

   (list fred george)

the variables FRED and GEORGE are evaluated and the LIST function is
called on their values.  The symbolic names are therefore not available
to your function.  So for starters, you would have to consider passing
in a list of the symbols:

  (define mylist (list 'fred 'george))

and then evaluate the symbols to get their value. Now, unfortunately, I
don't know enough Scheme to be able to say how to do it.  For global
variables in Common Lisp it would be fairly easy:

(defun greatfunc (symbol-list)
  (let ((answer ()))
    (dolist (sym symbol-list)
       (push (list sym (length (symbol-value sym)))
             answer))
     (nreverse answer)))   ; safe because we created new list structure.

       

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: George Neuner
Subject: Re: reporting on the "define"d
Date: 
Message-ID: <pf12b35g1e94dtd9la0ico4kr5f76t89t5@4ax.com>
On 01 Aug 2007 11:38:45 -0700, ···@sevak.isi.edu (Thomas A. Russ)
wrote:

>Drew Krause <········@mindspring.com> writes:
>
>> ..so I have a list of 'defined' lists & would like to report on
>> them. I'm confused as to how I might mix the name of each variable with
>> its information.
>> 
>> Here's an example:
>> 
>> (define fred (list 3 4 5))
>> (define george (list 1 2 3 4 5 6))
>> 
>> (define mylist (list fred george))
>> 
>> How to write a function that returns name + list length, e.g.
>> 
>> (greatfunc mylist) => ((fred 3) (george 5))
>> 
>> many thanks!
>
>As presented here, there is no way to retrieve the names FRED and GEORGE
>from the value of MYLIST.
>
>That is because, when you write
>
>   (list fred george)
>
>the variables FRED and GEORGE are evaluated and the LIST function is
>called on their values.  The symbolic names are therefore not available
>to your function.  So for starters, you would have to consider passing
>in a list of the symbols:
>
>  (define mylist (list 'fred 'george))
>
>and then evaluate the symbols to get their value. Now, unfortunately, I
>don't know enough Scheme to be able to say how to do it.


(map (lambda (n) (list n (length (eval n)))) mylist))

Unfortunately there's no (portable) equivalent of symbol-value in
Scheme.  For problems like this you have to use eval.

George
--
for email reply remove "/" from address
From: Rainer Joswig
Subject: Re: reporting on the "define"d
Date: 
Message-ID: <joswig-4024E0.17304701082007@news-europe.giganews.com>
In article <··············@corp.supernews.com>,
 Drew Krause <········@mindspring.com> wrote:

> ..so I have a list of 'defined' lists & would like to report on them. 
> I'm confused as to how I might mix the name of each variable with its 
> information.
> 
> Here's an example:
> 
> (define fred (list 3 4 5))
> (define george (list 1 2 3 4 5 6))
> 
> (define mylist (list fred george))
> 
> How to write a function that returns name + list length, e.g.
> 
> (greatfunc mylist) => ((fred 3) (george 5))
> 
> many thanks!

Is that Scheme? -> comp.lang.scheme

-- 
http://lispm.dyndns.org