Sml 函数在需要bool时返回字符串
这段代码怎么了Sml 函数在需要bool时返回字符串,sml,ml,Sml,Ml,这段代码怎么了 fun expd s:string=如果大小大于0,则为true,否则为false 我收到的错误是: - fun exnd s:string = if size(s) > a then true else false; stdIn:657.1-837.8 Error: unbound variable or constructor: a Error: right-hand-side of clause doesn't agree with function result
fun expd s:string=如果大小大于0,则为true,否则为false代码>
我收到的错误是:
- fun exnd s:string = if size(s) > a then true else false;
stdIn:657.1-837.8 Error: unbound variable or constructor: a
Error: right-hand-side of clause doesn't agree with function result type [tycon mismatch]
expression: bool
result type: string
为什么会发生这种情况?您需要在s:string
周围加上括号,因为现在string
被解释为函数的输出类型,而不是参数s
的类型:
fun expd (s:string) = if size(s) > 0 then true else false
顺便说一句,编译器可以推断这里的所有类型,而不需要任何类型注释。请尝试以下操作:
fun expd s = if size(s) > 0 then true else false
当然,您可以将此定义简化为:
fun expd s = size(s) > 0
在s:string
周围需要括号,因为现在string
被解释为函数的输出类型,而不是参数s
的类型:
fun expd (s:string) = if size(s) > 0 then true else false
顺便说一句,编译器可以推断这里的所有类型,而不需要任何类型注释。请尝试以下操作:
fun expd s = if size(s) > 0 then true else false
当然,您可以将此定义简化为:
fun expd s = size(s) > 0