对于此&#x27的正确Haskell语法是什么条款?
该语法的适当校正是什么?这是一个问题吗?我复制了 SO 上的答案。
我敢肯定,有更好的写作方法,但是我在免费代码方面确实没有任何好处。我是新手级别,并试图掌握非常基本的语法事物,这些内容继续通过进行大量的编码器练习来使我陷入困境。
import Data.List (findIndices)
basicOp :: Char -> Int -> Int -> Int
basicOp x y z = (operations !! (op x)) y z
where op x = head $ findIndices (== x) "+-*/" :: Char -> Int
operations = [(+), (-), (*), (div)] :: [(Int -> Int -> Int)]
当我将这样写为一个函数时,引用了另外两个功能。
import Data.List (findIndices)
operations :: [(Int -> Int -> Int)]
operations = [(+), (-), (*), (div)]
op :: Char -> Int
op x = head $ findIndices (== x) "+-*/"
basicOp :: Char -> Int -> Int -> Int
basicOp x y z = (operations !! (op x)) y z
带有Where子句返回以下错误的代码:
codewars.hs:103:34: error:
• Couldn't match expected type ‘Int’ with actual type ‘Char -> Int’
• Probable cause: ‘op’ is applied to too few arguments
In the second argument of ‘(!!)’, namely ‘(op x)’
In the expression: (operations !! (op x)) y z
In an equation for ‘basicOp’:
basicOp x y z
= (operations !! (op x)) y z
where
op x = head $ findIndices (== x) "+-*/" :: Char -> Int
operations = [(+), ....] :: [(Int -> Int -> Int)]
|
103 | basicOp x y z = (operations !! (op x)) y z
| ^^^^
codewars.hs:104:18: error:
• Couldn't match expected type ‘Char -> Int’ with actual type ‘Int’
• Possible cause: ‘($)’ is applied to too many arguments
In the expression: head $ findIndices (== x) "+-*/" :: Char -> Int
In an equation for ‘op’:
op x = head $ findIndices (== x) "+-*/" :: Char -> Int
In an equation for ‘basicOp’:
basicOp x y z
= (operations !! (op x)) y z
where
op x = head $ findIndices (== x) "+-*/" :: Char -> Int
operations = [(+), ....] :: [(Int -> Int -> Int)]
|
104 | where op x = head $ findIndices (== x) "+-*/" :: Char -> Int
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
我的原始尝试可能更容易阅读,但这只是尝试使用我尚不熟悉的更多Haskell构造的练习。
basicOp :: Char -> Int -> Int -> Int
basicOp oper x y
| oper == '+' = (+) x y
| oper == '-' = (-) x y
| oper == '*' = (*) x y
| oper == '/' = (div) x y
从@DanielWagner的评论中,这得到了改进到:
basicOp c = case c of
'+' -> (+)
'-' -> (-)
'*' -> (*)
'/' -> div
what is the appropriate correction for this syntax? Is it an issue white space? I copied the whitespace used in examples in LYAH, also tried other variations gleaned from answers on SO.
I'm sure there's better ways to write this, but I'm not really any good at point free code yet. I'm novice level and trying to master the very elementary syntax things that continue to trip me up by doing lots of codewars exercises.
import Data.List (findIndices)
basicOp :: Char -> Int -> Int -> Int
basicOp x y z = (operations !! (op x)) y z
where op x = head $ findIndices (== x) "+-*/" :: Char -> Int
operations = [(+), (-), (*), (div)] :: [(Int -> Int -> Int)]
when I write like this as one function making reference to two other functions it works.
import Data.List (findIndices)
operations :: [(Int -> Int -> Int)]
operations = [(+), (-), (*), (div)]
op :: Char -> Int
op x = head $ findIndices (== x) "+-*/"
basicOp :: Char -> Int -> Int -> Int
basicOp x y z = (operations !! (op x)) y z
the code with the where clause returns these errors:
codewars.hs:103:34: error:
• Couldn't match expected type ‘Int’ with actual type ‘Char -> Int’
• Probable cause: ‘op’ is applied to too few arguments
In the second argument of ‘(!!)’, namely ‘(op x)’
In the expression: (operations !! (op x)) y z
In an equation for ‘basicOp’:
basicOp x y z
= (operations !! (op x)) y z
where
op x = head $ findIndices (== x) "+-*/" :: Char -> Int
operations = [(+), ....] :: [(Int -> Int -> Int)]
|
103 | basicOp x y z = (operations !! (op x)) y z
| ^^^^
codewars.hs:104:18: error:
• Couldn't match expected type ‘Char -> Int’ with actual type ‘Int’
• Possible cause: ‘($)’ is applied to too many arguments
In the expression: head $ findIndices (== x) "+-*/" :: Char -> Int
In an equation for ‘op’:
op x = head $ findIndices (== x) "+-*/" :: Char -> Int
In an equation for ‘basicOp’:
basicOp x y z
= (operations !! (op x)) y z
where
op x = head $ findIndices (== x) "+-*/" :: Char -> Int
operations = [(+), ....] :: [(Int -> Int -> Int)]
|
104 | where op x = head $ findIndices (== x) "+-*/" :: Char -> Int
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my original attempt is probably easier to read, but this is just an exercise to try and use more Haskell constructs which I'm not familiar with yet.
basicOp :: Char -> Int -> Int -> Int
basicOp oper x y
| oper == '+' = (+) x y
| oper == '-' = (-) x y
| oper == '*' = (*) x y
| oper == '/' = (div) x y
From the comments, by @DanielWagner, this is improved to:
basicOp c = case c of
'+' -> (+)
'-' -> (-)
'*' -> (*)
'/' -> div
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
op x
表达式的主体类型是int
,而不是char-> int
。您还应将操作定位
在与op
的同一中
列 ://hackage.haskell.org/package/base-4.16.1.0/docs/prelude.html#v:lookup“ rel =“ nofollow noreferrer”>
lookup :: eq a => a - > [(a,b)] - >也许b
:其中
…
是一个表达式The type of the body of the
op x
expression is anInt
, not aChar -> Int
. You should also positionoperations
at the same column asop
, so:But the types are not necessary, you can simplify this to:
and work with
lookup :: Eq a => a -> [(a, b)] -> Maybe b
:where
…
is an expression that is then evaluated if the key is not found来提供该函数本身的类型。
请注意,您 can 在单独的行上或通过提供显式lambda表达式作为
op op
的值Note that you can provide a type for the function itself, either on a separate line
or by providing an explicit lambda expression as the value for
op
.哦,删除
op
和操作的类型声明
工作。我认为有时将类型声明“内联”放入类型声明是可以的。
Oh, removing the type declarations for
op
andoperations
works.I thought it was ok (helpful even) to put type declarations "inline" sometimes.