I'm playing around with guile to try and get familiar with pure functional programming concepts. Before I can do anything useful with any language, I need to understand some basic data structures and how to manipulate them effectively... in particular, enumerable data structures.
I can iterate a list like this (I'm not sure if I'm indenting this correctly or not):
(map (lambda (v)
(display (string-append v "\n"))
'(1 2 3))
=>
1
2
3
What does a hash table/hash map look like in scheme? Is there a real data structure to represent one, or do it come down to making a list of lists? In which case, how do you get the key and value as separate variables from the inner list?
Obviously this is wrong, since the lambda expects one value, not two:
(map (lambda (key value)
(display (string-append key " => " value))
'('("one" 1) '("two" 2) '("three" 3)))
The Ruby equivalent of what I'm trying to do would be:
{ "one" => 1, "two" => 2, "three" => 3 }.map do |key, value|
puts "#{key} => #{value}"
end
If you are using R6RS hashtables, you can use the hashtable-keys
and hashtable-entries
functions.
If you're using Guile's native hashtables, you can use hash-map->list
, hash-for-each
, hash-for-each-handle
, or hash-fold
.
So for your example, using Guile's hash-for-each
, you'd do:
(use-modules (ice-9 hash-table))
(define my-hash (make-hash-table))
(hash-set! my-hash "one" 1)
(hash-set! my-hash "two" 2)
(hash-set! my-hash "three" 3)
(hash-for-each (lambda (key value)
(format #t "~a => ~a~%" key value))
my-hash)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With