SML - Creating dictionary that maps keys to values

╄→尐↘猪︶ㄣ 提交于 2019-12-05 18:36:58

You can reason on the signature of the function:

val insert = fn: (string * int) -> dict -> dict

When you supply key, value and a dictionary d, you would like to get back a new dictionary d'. Since dict is string -> int option, d' is a function takes a string and returns an int option.

Suppose you supply a string s to that function. There are two cases which could happen: when s is the same as key you return the associated value, otherwise you return a value by looking up d with key s.

Here is a literal translation:

fun insert (key, value) d = fn s => if s = key then SOME value
                                    else d s
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!