Jump to content

Associative array/Creation: Difference between revisions

Line 576:
 
=={{header|Haskell}}==
Binary trees:
{{works with|GHC}}
<lang haskell>import Data.Map
Line 583 ⟶ 584:
ans = Data.Map.lookup "key2" dict -- evaluates to "val2"
</lang>
 
It is also possible to use association lists (lists of pairs). It is inefficient (O(n) lookup), but simple.
<lang haskell>dict = [("key1","val1"), ("key2","val2")]
 
ans = lookup "key2" dict -- evaluates to Just "val2"
</lang>
 
GHC also has an imperative hash table implementation in the <code>Data.HashTable</code> module. However, it is cumbersome to use because it is in the IO monad, and and it requires the user to supply a hash function.
 
=={{header|Icon}} and {{header|Unicon}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.