inter :: [Integer] -> [Integer] -> [Integer]\ninter s1 s2 = [s | s <- s2, s `elem` s1]\n\ninter [5, 6, 3, 1], [5, 2, 1, 3]\n\nmain = return()\nRun Code Online (Sandbox Code Playgroud)\n我不确定如何测试上述实现,我试图在这里传递两个不同的列表。
\n我不断得到:
\nGHCi, version 8.10.6: https://www.haskell.org/ghc/ :? for help\nLoaded GHCi configuration from /home/runner/University-Labs/.ghci\n[1 of 1] Compiling Main ( Main.hs, interpreted )\n\nMain.hs:4:19: error: parse error on input \xe2\x80\x98,\xe2\x80\x99\n |\n4 | inter [5, 6, 3, 1], [5, 2, 1, 3]\n | ^\nFailed, no modules loaded.\n\xee\xba\xa7 \n<interactive>:1:1: error:\n \xe2\x80\xa2 Variable not in scope: main\n \xe2\x80\xa2 Perhaps you meant \xe2\x80\x98min\xe2\x80\x99 (imported from Prelude)\n\xee\xba\xa7 ^C\xee\xba\xa7 Leaving GHCi.\nrepl process died unexpectedly: \nGHCi, version 8.10.6: https://www.haskell.org/ghc/ :? for help\nLoaded GHCi configuration from /home/runner/University-Labs/.ghci\n\xee\xba\xa7 \n\xee\xba\xa7 \nRun Code Online (Sandbox Code Playgroud)\n
有两件事是错误的:
应用多个参数并没有用,,您可以将其省略。
您不能在文件的顶层使用表达式(如函数应用程序),而是可以将其放入函数中,main如下所示:
inter :: [Integer] -> [Integer] -> [Integer]
inter s1 s2 = [s | s <- s2, s `elem` s1]
main = do
print (inter [5, 6, 3, 1] [5, 2, 1, 3])
-- you can do more here if you want
Run Code Online (Sandbox Code Playgroud)