Jump to content

Hash join: Difference between revisions

1,333 bytes added ,  8 years ago
Added EchoLisp
(Added EchoLisp)
Line 396:
</pre>
 
=={{header|EchoLisp}}==
Since this is a real, professional application, we build the hash tables in permanent (local) storage.
<lang lisp>
(define ages '((27 "Jonah") (18 "Alan") (28 "Glory") (18 "Popeye") (28 "Alan")))
(define nemesis '(("Jonah" "Whales") ("Jonah" "Spiders") ("Alan" "Ghosts") ("Alan" "Zombies") ("Glory" "Buffy")))
 
;; table: table name
;; source : input list
;; key-proc : procedure returning the join value ('name' in this task)
 
(define (table-hash table source key-proc )
(local-make-store table)
(for ((r source))
(local-put-value
(key-proc r)
(append (list r) (local-get-value (key-proc r) table)) table)))
 
;; build the two tables
(define-syntax-rule (second record) (cadr record))
(define (key-name-age record) (second record))
(table-hash 'AGES ages key-name-age)
 
(define (key-nemesis-name record) (first record))
(table-hash 'NEMESIS nemesis key-nemesis-name)
 
;; join
(for* ((k (local-keys 'AGES))
(a (local-get-value k 'AGES))
(n (local-get-value k 'NEMESIS)))
(writeln a n))
</lang>
{{out}}
<lang lisp>
(28 "Alan") ("Alan" "Zombies")
(28 "Alan") ("Alan" "Ghosts")
(18 "Alan") ("Alan" "Zombies")
(18 "Alan") ("Alan" "Ghosts")
(28 "Glory") ("Glory" "Buffy")
(27 "Jonah") ("Jonah" "Spiders")
(27 "Jonah") ("Jonah" "Whales")
</lang>
 
=={{header|ECL}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.