From: Thomas Guettler
Subject: Hashtables in Lisp
Date: 
Message-ID: <am7580$g39$02$1@news.t-online.com>
Hi!

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.

  thomas

From: Tim Bradshaw
Subject: Re: Hashtables in Lisp
Date: 
Message-ID: <ey3bs6wx119.fsf@cley.com>
* Thomas Guettler wrote:
> 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")))
        `(loop with ,htn = (make-hash-table ,@args-to-mht)
             for ,an on (list ,@inits) by #'cddr
             do (setf (gethash (first ,an) ,htn) (second ,an))
             finally (return ,htn))))

Then:

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

does what you think.

--tim  
From: David Hanley
Subject: Re: Hashtables in Lisp
Date: 
Message-ID: <281e82b2.0209171234.786265d0@posting.google.com>
Thomas Guettler <···········@thomas-guettler.de> wrote in message news:<···············@news.t-online.com>...
> 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))
    ,@(mapcar (lambda(pair)`(setf (gethash ,(first pair) ,name)
,(second pair))) items)))

Now, you can write

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

dave
From: Frode Vatvedt Fjeld
Subject: Re: Hashtables in Lisp
Date: 
Message-ID: <2hit14erra.fsf@vserver.cs.uit.no>
Thomas Guettler <···········@thomas-guettler.de> writes:

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

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

> 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
From: Jacek Generowicz
Subject: Re: Hashtables in Lisp
Date: 
Message-ID: <tyfit14aj7q.fsf@pcitapi22.cern.ch>
Frode Vatvedt Fjeld <······@acm.org> writes:

>   #.(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 ?
From: james anderson
Subject: Re: Hashtables in Lisp
Date: 
Message-ID: <3D874DA5.3C98B81C@setf.de>
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.

Jacek Generowicz wrote:
> 
> ...
>
> ... 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 ?

...
From: Jacek Generowicz
Subject: Re: Hashtables in Lisp
Date: 
Message-ID: <tyflm5z91te.fsf@pcitapi22.cern.ch>
james anderson <··············@setf.de> writes:

> 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.
From: Christopher Browne
Subject: Re: Hashtables in Lisp
Date: 
Message-ID: <am7f0f$3dlc4$5@ID-125932.news.dfncis.de>
Thomas Guettler <···········@thomas-guettler.de> wrote:
> 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.
-- 
(reverse (concatenate 'string ········@" "enworbbc"))
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/>
From: Paul Foley
Subject: Re: Hashtables in Lisp
Date: 
Message-ID: <m2fzw77847.fsf@mycroft.actrix.gen.nz>
On 17 Sep 2002 14:42:24 GMT, Christopher Browne wrote:

> Thomas Guettler <···········@thomas-guettler.de> wrote:
>> 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
  (concatenate 'string "Paul Foley " "<mycroft" '(··@) "actrix.gen.nz>"))
From: Jeff Sandys
Subject: Re: Hashtables in Lisp
Date: 
Message-ID: <3D874F92.D68B920F@juno.com>
Thomas Guettler wrote:
> 
> 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
From: Marco Baringer
Subject: Re: Hashtables in Lisp
Date: 
Message-ID: <m2ptvcy7r2.fsf@bese.it>
Thomas Guettler <···········@thomas-guettler.de> writes:

> 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