我正在尝试制作一个将表单字符串映射"SXSXSSSXXX"
到instruction list
s [S,X,S,X,S,S,S,X,X,X]
where的函数
datatype instruction = S | X
type sequence = instruction list
我正在使用map
并String.explode : string -> char list
实现这一目标:
fun str_to_sequence(s: string) =
map(fn c => if c = #"S" then S else X, String.explode(s))
map 的第一个参数是 type char -> instruction
,第二个参数是 type char list
。map : ('a -> 'b) -> 'a list -> 'b list
这与, where 'a = char
,的类型签名匹配'b = instruction
,但是,当我尝试编译它时,我得到以下 tycon 不匹配:
- use "stack_sequences.sml";
[opening stack_sequences.sml]
[autoloading]
[library $SMLNJ-BASIS/basis.cm is stable]
[autoloading done]
stack_sequences.sml:5.5-5.62 Error: operator and operand don't agree [tycon mismatch]
operator domain: 'Z -> 'Y
operand: (char -> instruction) * char list
in expression:
map ((fn c => if <exp> then <exp> else <exp>),String.explode s)
uncaught exception Error
raised at: ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
../compiler/TopLevel/interact/evalloop.sml:44.55
../compiler/TopLevel/interact/evalloop.sml:296.17-296.20
代码有什么问题?我找不到类型不匹配。