Haskell列出了错误

时间:2018-06-11 16:08:30

标签: haskell

我有这个:

varCheck :: Defs -> [Error]
nameChecking :: Defs -> Body -> [Error]

checkProgram :: Program -> [Error]
checkProgram (Program n d b)  = do
  dupE <- varCheck d
  undefE <- nameChecking d b
  return undefE ++ dupE

当我尝试编译时,我得到了这个错误:

Couldn't match expected type ‘[Error]’ with actual type ‘Error’
In the second argument of ‘(++)’, namely ‘dupE’
In a stmt of a 'do' block: return undefE ++ dupE

我真的不讨厌,为什么它将dupE视为单个元素。 谢谢!

1 个答案:

答案 0 :(得分:3)

当你有

v <- expr

do块中,expr必须包含m a类型(对于某些monad m并输入a)和v然后类型为a

例如,在

str <- getLine

getLine :: IO Stringstr :: String(我们的monad mIO播放,aString播放。

在您的代码中

dupE <- varCheck d

我们得到varCheck d :: [Error][Error][] Error相同,因此m = []a = Error;因此dupE :: Error

如果您只想连接从varChecknameChecking返回的列表,则使用<-毫无意义,checkProgram (Program n d b) = do let dupE = varCheck d let undefE = nameChecking d b undefE ++ dupE 专门用于从列表中“提取”单个元素(或者循环遍历所有元素,真的)。相反,你可以做

<-

但是现在你根本没有使用do,那么为什么还要使用checkProgram (Program n d b) = let dupE = varCheck d undefE = nameChecking d b in undefE ++ dupE 块呢?

where

或者,如果您更喜欢checkProgram (Program n d b) = undefE ++ dupE where dupE = varCheck d undefE = nameChecking d b

public class RegisterScene : MonoBehaviour {
    public GameObject errorPanel; // Error panel

    void Awake()
    {
        errorPanel = GameObject.FindGameObjectWithTag("errorPanel"); // Error panel Tag
        Debug.Log("Error Panel: " + errorPanel); // Outputs "ErrorPanel" as named in Inspector
        errorPanel.gameObject.SetActive(false); // Have to hide on scene Awake - throws NullReferenceError here
        //errorPanel.gameObject.SetActive(true); // This works as a test case
    }

    // Register button clicked
    public void Register ()
    {
        // If error 
        showErrorPanel("You must complete all sections");
    }

    // Show error panel
    public void showErrorPanel(string errorMessage)
    {
        Debug.Log("Showing error panel");
        errorPanel.gameObject.SetActive(true); // Working - shows error panel

        // Confirmation button on error panel clicked
        hideErrorPanel();
    }

    // Hide error panel
    public void hideErrorPanel()
    {
        Debug.Log("Hiding error panel");
        errorPanel.gameObject.SetActive(false); // Working - hides error panel
    }
}