表达式中的语法 - Haskell [英] syntax in expression - Haskell
问题描述
我写了这段代码:
$ pre $ import $ Data.List
inputIndex :: [String] - > [字符串] - > Bool
inputIndex listx input =和[x`elem` listx | x < - input]
inputIndex = if inputIndex == true
then putStrLn(ok)
如果语句没有,它会正常工作,但是当我把
if
语句显示以下错误:
$ b
表达式中的语法错误(意外的`} ',可能是由于布局不好)
我在这里做错了什么?
感谢
- 您需要一个else子句。
-
True
必须是大写。 -
inputIndex
必须总是带两个参数(现在它不是,在最后一种情况下)。
我想你想要这样的东西...
inputIndex :: [String] - > [字符串] - > IO()
inputIndex listx input = if inputIndex'listx input
then putStrLn(ok)
else putStrLn(not ok)
where
inputIndex' :: [String] - > [字符串] - > Bool
inputIndex'listx input = and [x`elem` listx | x < - input]
(在这里,我用一个几乎相同的名字定义了一个新函数,通过附加一个主要/撇号来定义它,在其中
子句中定义它,它只对外部的inputIndex
函数,如果你愿意的话,你可以把它称为助手函数,我也可以选择一个完全不同的名字,但我没有创造力。 p>
你也可以把它压缩到以下内容中(这也是更一般的):
allPresent ::(Eq t)=> [t] - > [t] - > IO()
allPresent xs ys = putStrLn(if和[y`elem` xs | y < - ys] thenokelsenot ok)
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 theif
statement the following error is shown:Syntax error in expression (unexpected `}', possibly due to bad layout)
What am I doing wrong here?
Thanks
解决方案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 outerinputIndex
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")
这篇关于表达式中的语法 - Haskell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!