【发布时间】:2019-04-28 06:43:58
【问题描述】:
我是函数式编程的新手,我想创建自己的结构/签名,称为 Dictionary。到目前为止,我在名为 dictionary-en.sml 的文件中有这个:
(* The signature DICTIONARY defines a type and a programming interface for
the dictionary data structure. The data structure allows us to store
data in the form of (key, value) pairs and to query the data using a key. *)
signature DICTIONARY =
sig
(* The structure has to implement a dictionary type. It defines key type,
which has to support equality checking, and a value type for the data
stored in the dictionary. *)
type (''key, 'value) dict
(* Creates an empty dictionary. *)
val empty: (''key, 'value) dict
(* Returns true if a key exists in the dictionary. *)
val exists: (''key, 'value) dict -> ''key -> bool
end
我在文件 solution.sml 中有这个:
structure Dictionary :> DICTIONARY =
struct
type (''key, 'value) dict = (''key * 'value) list
val empty = []
fun exists dict key =
case dict of
[] => false
| (k, _ )::rep => if k = key
then true
else exists rep key
end
但我不知道如何使用它。 当我在 REPL 中写道:
- Dictionary.exists [(3,"c"), (5, "e"), (7, "g")] 3;
我收到了这个错误:
stdIn:1.2-3.7 Error: operator and operand do not agree [tycon mismatch]
operator domain: (''Z,'Y) Dictionary.dict
operand: ([int ty] * string) list
in expression:
Dictionary.exists ((3,"c") :: (5,"e") :: (<exp>,<exp>) :: nil)
有人可以帮帮我吗?我不知道我做错了什么。
【问题讨论】:
标签: functional-programming sml smlnj