表达式中的语法

编程入门 行业动态 更新时间:2024-10-22 10:52:19
表达式中的语法 - Haskell(syntax in expression - Haskell)

我是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")

更多推荐

本文发布于:2023-08-06 19:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454097.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   语法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!