2016-11-28 4 views
0

このコードは何が問題になりますか?boolが期待されるときに関数が文字列を返します

fun expd s:string = if size(s) > 0 then true else 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 

ですが、なぜでしょうか?今stringは、関数の出力タイプとパラメータsのないタイプとして解釈されるため、

答えて

4

あなたは、s:stringを括弧で囲む必要があります。

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 
関連する問題