我是Haskell的新手!! 我写了这段代码:
import Data.List inputIndex :: [String] -> [String] -> Bool inputIndex listx input = and [x `elem` listx |x <- input] inputIndex = if inputIndex == true then putStrLn ("ok")没有if语句它工作正常但是当我把if语句显示时出现以下错误:
表达式中的语法错误(意外的`}',可能是由于布局错误)
我在这做错了什么?
谢谢
Im new to Haskell!! I wrote this code:
import Data.List inputIndex :: [String] -> [String] -> Bool inputIndex listx input = and [x `elem` listx |x <- input] inputIndex = if inputIndex == true then putStrLn ("ok")It works fine without the if statement but when I put the if statement the following error is shown:
Syntax error in expression (unexpected `}', possibly due to bad layout)
What am I doing wrong here?
Thanks
最满意答案
这里有几件事是错的:
你需要一个else子句。 True必须大写。 inputIndex必须总是带两个参数(现在它不会,在最后一种情况下)。我猜你想要这样的东西......
inputIndex :: [String] -> [String] -> IO () inputIndex listx input = if inputIndex' listx input then putStrLn ("ok") else putStrLn ("not ok") where inputIndex' :: [String] -> [String] -> Bool inputIndex' listx input = and [x `elem` listx |x <- input](这里我通过附加一个素数/撇号来定义一个名称几乎相同的新函数。通过在where子句中定义它,它只对外部inputIndex函数可见。你可以将它称为辅助函数,如果你我也可以选择一个完全不同的名字,但我没有创意。)
您还可以将其浓缩为以下(这也更通用):
allPresent :: (Eq t) => [t] -> [t] -> IO () allPresent xs ys = putStrLn (if and [y `elem` xs | y <- ys] then "ok" else "not ok")A couple of things are wrong here:
You will need an else clause. True must be capitalized. inputIndex must always take two arguments (right now it does not, in the last case).I guess you want something like this...
inputIndex :: [String] -> [String] -> IO () inputIndex listx input = if inputIndex' listx input then putStrLn ("ok") else putStrLn ("not ok") where inputIndex' :: [String] -> [String] -> Bool inputIndex' listx input = and [x `elem` listx |x <- input](Here I defined a new function with a near-identical name, by appending a prime/apostrophe. By defining it in the where clause, it is only visible to the outer inputIndex function. You can call this a helper-function, if you will. I could also have chosen a completely different name, but I'm uncreative.)
You could also condense this to the following (which is also more general):
allPresent :: (Eq t) => [t] -> [t] -> IO () allPresent xs ys = putStrLn (if and [y `elem` xs | y <- ys] then "ok" else "not ok")更多推荐
发布评论