Immutable dictionaries in Haskell

2

You need to implement immutable dictionaries using only functions without any other data types (custom or built-in). The dictionary must be indexed by any data type of class Eq and contain values of arbitrary type, but all the values in a dictionary should be of the same type.

Here are some examples of usage:

dict `access` True -- returns value of key True
dict `access` "y"  -- returns value of key "y"
(update "x" 10 dict) `access` "x" -- returns 10 for any dict

Curly Brace

Posted 2014-05-15T10:01:55.543

Reputation: 43

Question was closed 2014-05-20T07:17:55.813

1This is a site for programming contests, and contests need criteria to select the winners. What makes one correct implementation better than another? – Peter Taylor – 2014-05-15T10:08:34.063

The amount of code is the criteria (less is better). – Curly Brace – 2014-05-15T10:12:01.893

Answers

3

update key1 value obj = \key2 -> if key2 == key1 then value else obj key2
access = id

Given the above definitions, your examples should work. Also, you can define an empty dictionary as const x where x is the default value for keys not in the dictionary.

vlad

Posted 2014-05-15T10:01:55.543

Reputation: 146