[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Hashtable iteration: performing early breaks

William James

10/16/2015 9:57:00 PM

Frode Vatvedt Fjeld wrote:

> (defun confirm-loaded (n-val)
> "Scan the current contents of *data* and confirm whether or not n-val
> already exists."
> (let ((match-key nil))
> (prog
> (maphash #'(lambda (k v)
> (when (equalp n-val v)
> (setf match-key k)
> (go done)))
> *data*)
> done)
> match-key))

Gauche Scheme:

(use gauche.generator :only (x->generator))

(define (hash-table-val-exists? ht val)
(generator-find
(lambda (k.v) (equal? val (cdr k.v)))
(x->generator ht)))

--
What I would most desire would be the separation of the white and black
races. --- A. Lincoln, July 17, 1858
1 Answer

William James

10/31/2015 11:34:00 AM

0

WJ wrote:

> Frode Vatvedt Fjeld wrote:
>
> > (defun confirm-loaded (n-val)
> > "Scan the current contents of data and confirm whether or not n-val
> > already exists."
> > (let ((match-key nil))
> > (prog
> > (maphash #'(lambda (k v)
> > (when (equalp n-val v)
> > (setf match-key k)
> > (go done)))
> > data)
> > done)
> > match-key))
>
> Gauche Scheme:
>
> (use gauche.generator :only (x->generator))
>
> (define (hash-table-val-exists? ht val)
> (generator-find
> (lambda (k.v) (equal? val (cdr k.v)))
> (x->generator ht)))

The problem is to find the key corresponding to a value
without scanning the entire hash-table.

MatzLisp (Ruby):

h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }
h.key(200) #=> "b"
h.key(300) #=> "c"
h.key(999) #=> nil

--
A separation of the races is the only perfect preventive of amalgamation; but
as an immediate separation is impossible, the next best thing is to keep them
apart where they are not already together. If white and black people never get
together in Kansas, they will never mix blood in Kansas.
--- A. Lincoln, June 26, 1857