Hash from two arrays: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
Line 2,156: Line 2,156:


=={{header|Scheme}}==
=={{header|Scheme}}==
Using [http://srfi.schemers.org/srfi-69/srfi-69.html SRFI 69]:
=== Using [http://srfi.schemers.org/srfi-69/srfi-69.html SRFI 69] ===
<lang scheme>(define (lists->hash-table keys values . rest)
<lang scheme>(define (lists->hash-table keys values . rest)
(apply alist->hash-table (map cons keys values) rest))</lang>
(apply alist->hash-table (map cons keys values) rest))</lang>

=== Using association lists ===
<lang scheme>;; Using SRFI-1, R6RS, or R7RS association lists.

;; Because the task calls for ‘arrays’, I start with actual arrays
;; rather than lists.
(define array1 (vector "a" "b" "c" "d"))
(define array2 (vector 1 2 3 4))

;; Making the hash is just a simple
(define dictionary (map cons (vector->list array1)
(vector->list array2)))

;; Now you can look up associations with assoc.
(write (assoc "b" dictionary)) (newline)
(write (assoc "d" dictionary)) (newline)

;; USING CHICKEN 5 SCHEME, OUTPUT FROM EITHER
;; ‘csc -R srfi-1 thisprog.scm && ./thisprog’
;; OR ‘csc -R r7rs thisprog.scm && ./thisprog’,
;; AND ALSO TESTED IN CHEZ SCHEME (R6RS):
;;
;; ("b" . 2)
;; ("d" . 4)
;;</lang>


=={{header|Seed7}}==
=={{header|Seed7}}==