Hashtables in Lisp 
Author Message
 Hashtables in Lisp

Hi!

Is there a way to define hash tables like
in python:

mydict={
  "foo": [1, 2, 3],
  "bar": [4, 5, 6]

Quote:
}

The way described at http://www.*-*-*.com/
is more verbose.

  thomas



Sat, 05 Mar 2005 19:57:47 GMT  
 Hashtables in Lisp

Quote:

> Is there a way to define hash tables like in python:

CL doesn't define a readable syntax for hash-tables.

Quote:
> mydict={
>   "foo": [1, 2, 3],
>   "bar": [4, 5, 6]
> }

The closest thing in vanilla CL I suppose would be

  #.(loop with h = (make-hash-table :test #'equal)
       for (k v) in (("foo" (1 2 3))
                     ("bar" (4 5 6)))
       do (setf (gethash k h) v)
       finally (return h))

If this is something you really need and would use a lot, you'd
probably install it as a reader macro, to yield a syntax something
like this

  #h(("foo" (1 2 3)) ("bar" (4 5 6)))

--
Frode Vatvedt Fjeld



Sat, 05 Mar 2005 20:15:53 GMT  
 Hashtables in Lisp

Quote:

> mydict={
>   "foo": [1, 2, 3],
>   "bar": [4, 5, 6]
> }

Would something like this satisfy you?

    (defun make-hash (&rest args &key (initial-contents '() icp)
                            &allow-other-keys)
      (if (not icp)
          (apply #'make-hash-table args)
        (let ((table (apply #'make-hash-table
                            (loop for a on args by #'cddr
                                  unless (eql (first a) ':initial-contents)
                                  collect (first a) and collect (second a)))))
          (loop for (k v) in initial-contents
                do (setf (gethash k table) v))
          table)))

(make-hash :initial-contents '((a 1) (b 2) (c 3))) does what you
think.  More macrified versions are also possible:

    (defmacro hash ((&rest args-to-mht) &body inits)
      (let ((htn (make-symbol "HT"))
            (an (make-symbol "A")))


             do (setf (gethash (first ,an) ,htn) (second ,an))
             finally (return ,htn))))

Then:

(hash ()
  1 2
  'a '(1 2 3))

does what you think.

--tim  



Sat, 05 Mar 2005 20:18:10 GMT  
 Hashtables in Lisp

Quote:
>   #.(loop with h = (make-hash-table :test #'equal)
>        for (k v) in (("foo" (1 2 3))
>                      ("bar" (4 5 6)))
>        do (setf (gethash k h) v)
>        finally (return h))

> If this is something you really need and would use a lot, you'd
> probably install it as a reader macro, to yield a syntax something
> like this

>   #h(("foo" (1 2 3)) ("bar" (4 5 6)))

... this reminds me of something I've been meaning to ask for ages;
CLHS has the following to say about print-object:

  [...]
  Users may write methods for print-object for their own classes [...]

which does not _necessarily_ imply that they may not write them for
built-in types (and I haven't found anything that explicitly forbid
it), but it seems that this cannot be done.

Is there a way to make the printed representation of hash tables match
a read-syntax such as the above ?



Sat, 05 Mar 2005 20:34:01 GMT  
 Hashtables in Lisp

Quote:

> Is there a way to define hash tables like
> in python:

> mydict={
>   "foo": [1, 2, 3],
>   "bar": [4, 5, 6]
> }

> The way described at http://www.lispworks.com/reference/HyperSpec/Body/18_aa.htm
> is more verbose.

There's not a default "syntax" for this; it is certainly easy enough
to write up something like:

(loop for (key . value) in '(("foo" . (1 2 3)) ("bar" . (4 5 6)))
      do (setf (gethash mydict key) value))

I seem to recall Erik Naggum having written up a readtable macro that
did something a /little/ smarter than this.

What is unfortunate, of course, is that there's not a decent way of
using (format T "~S~%" mydict) to generate something that can be read
back in.
--

http://www3.sympatico.ca/cbbrowne/wp.html
Rules  of the  Evil  Overlord #112.  "I  will not  rely entirely  upon
"totally  reliable"  spells  that  can be  neutralized  by  relatively
inconspicuous talismans." <http://www.eviloverlord.com/>



Sat, 05 Mar 2005 22:42:24 GMT  
 Hashtables in Lisp

Quote:

> Hi!

> Is there a way to define hash tables like
> in python:

Erik Naggum provided an alternate perl-like syntax to defining hash
tables. see
http://groups.google.it/groups?&selm=3222747467111424%40naggum.net

if you really want python syntax it's not hard to change it.

i found this to also be a beautiful example of a use of the loop macro
to clearly describe a rather complex piece of iteration.

however, as Mr. Naggum points out in the message you want to consider
alists and plists before you decide on using hash tables (the ability
to "shadow" values, can be very usefull).

CL has so many data builtin data structures, and, maybe more
importantly, "popular" languages have so few built in data structures,
that's at first it's hard to know what to use, or even to realize
there's more to life than the list, the hash table and the string.

--
-Marco
Ring the bells that still can ring.
Forget your perfect offering.
There is a crack in everything.
That's how the light gets in.
     -Leonard Cohen



Sat, 05 Mar 2005 23:07:45 GMT  
 Hashtables in Lisp

item 19 among the conformance constrains (see
http://www.lispworks.com/reference/HyperSpec/Body/11_abab.htm) suggests that
such a method could not be used in a conforming program.

Quote:

> ...

> ... this reminds me of something I've been meaning to ask for ages;
> CLHS has the following to say about print-object:

>   [...]
>   Users may write methods for print-object for their own classes [...]

> which does not _necessarily_ imply that they may not write them for
> built-in types (and I haven't found anything that explicitly forbid
> it), but it seems that this cannot be done.

> Is there a way to make the printed representation of hash tables match
> a read-syntax such as the above ?

...


Sat, 05 Mar 2005 23:43:39 GMT  
 Hashtables in Lisp

Quote:

> Is there a way to define hash tables like
> in python:

> mydict = {"foo": [1, 2, 3], "bar": [4, 5, 6]}

I usually use associated lists or paired lists
for these simple dictionaries.  You need lots
of items to benefit from using hash tables.

        ;;; associated list
USER(1): (setq b '((foo . (1 2 3)) (bar . (4 5 6))))
((FOO 1 2 3) (BAR 4 5 6))
USER(2): (cdr (assoc 'foo b))
(1 2 3)

        ;;; paired list
USER(3): (setq c '(foo (1 2 3) bar (4 5 6)))
(FOO (1 2 3) BAR (4 5 6))
USER(4): (getf c 'foo)
(1 2 3)

You can use the setf method to alter the values
in the lists.

USER(5): (setf (getf c 'bar) '(7 8 9))
(7 8 9)
USER(6): (getf c 'bar)
(7 8 9)
USER(7): (setf (getf c 'baz) '(a b c))
(A B C)
USER(8): (getf c 'baz)
(A B C)
USER(9): c
(BAZ (A B C) FOO (1 2 3) BAR (7 8 9))

Thanks,
Jeff Sandys



Sat, 05 Mar 2005 23:51:46 GMT  
 Hashtables in Lisp

Quote:

> Hi!

> Is there a way to define hash tables like
> in python:

> mydict={
>   "foo": [1, 2, 3],
>   "bar": [4, 5, 6]
> }

A macro should be pretty easy:

(defmacro makehash( name items )
  `(progn (defparameter ,name (make-hash-table))

,(second pair))) items)))

Now, you can write

(makehash this ((1 2) (3 4)))

dave



Sun, 06 Mar 2005 04:34:10 GMT  
 Hashtables in Lisp

Quote:

> item 19 among the conformance constrains (see
> http://www.lispworks.com/reference/HyperSpec/Body/11_abab.htm) suggests that
> such a method could not be used in a conforming program.

Bingo !

Thank you.



Sun, 06 Mar 2005 15:47:25 GMT  
 Hashtables in Lisp

Quote:

>> Is there a way to define hash tables like
>> in python:

>> mydict={
>> "foo": [1, 2, 3],
>> "bar": [4, 5, 6]
>> }

>> The way described at http://www.lispworks.com/reference/HyperSpec/Body/18_aa.htm
>> is more verbose.
> There's not a default "syntax" for this; it is certainly easy enough
> to write up something like:
> (loop for (key . value) in '(("foo" . (1 2 3)) ("bar" . (4 5 6)))
>       do (setf (gethash mydict key) value))
> I seem to recall Erik Naggum having written up a readtable macro that
> did something a /little/ smarter than this.
> What is unfortunate, of course, is that there's not a decent way of
> using (format T "~S~%" mydict) to generate something that can be read
> back in.

As long as *print-pretty* is set, you can set up *print-pprint-dispatch*
to do whatever you like.

--
If that makes any sense to you, you have a big problem.
                                      -- C. Durance, Computer Science 234
(setq reply-to



Sun, 06 Mar 2005 21:14:16 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. String HashTables

2. another problem with hashtables

3. weak hashtables in LW 4.2

4. string or int hashtables

5. Lisp,LIsp,LISp,LISP...

6. Lisp,LIsp,LISp,LISP...

7. New Common Lisp, Lisp-to-C translation, Lisp library for C

8. LISP - The Spirit of Lisp - Transparency!

9. The Lisp Journal ( was Re: ANN: Yadda Lambda, Lisp magazine)

10. New Common Lisp and Lisp-to-C translator ANNOUNCEMENT

11. Running Koza's GP1 Lisp code on Common Lisp

12. 2nd RFD: comp.org.lisp-users and comp.std.lisp

 

 
Powered by phpBB® Forum Software