Hash from two arrays: Difference between revisions

Line 2,184:
;; ("d" . 4)
;;</lang>
 
=== Using [[Associative_array/Creation#A_persistent_associative_array_from_scratch|''persistent'' associative arrays]] ===
 
You need to compile this code along with the [[Associative_array/Creation#A_persistent_associative_array_from_scratch|persistent associative arrays]] code. And, yes, the hash function for that implementation ''can'' return floating point numbers, the way the one here does.
 
<lang scheme>(import (associative-arrays))
 
;; 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))
 
(define (silly-hashfunc s)
(define h 1234)
(do ((i 0 (+ i 1)))
((= i (string-length s)))
(set! h (+ h (char->integer (string-ref s i)))))
(sqrt (/ (* h 101) 9.80665)))
 
;; Here is the making of the associative array:
(define dictionary (assoc-array silly-hashfunc))
(vector-for-each (lambda (key data)
(set! dictionary
(assoc-array-set dictionary key data)))
array1 array2)
 
(display "Looking up \"b\" and \"d\": ")
(write (assoc-array-ref dictionary "b"))
(display " ")
(write (assoc-array-ref dictionary "d"))
(newline)
 
;; Output:
;;
;; Looking up "b" and "d": 2 4
;;</lang>
 
Side note: association lists can be used in either a persistent or non-persistent manner.
 
=={{header|Seed7}}==
1,448

edits