這種語法的適當更正是什么?這是一個問題空白嗎?我復制了LYAH中示例中使用的空格,還嘗試了從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
uj5u.com熱心網友回復:
op x運算式主體的型別是 a Int,而不是 a Char -> Int。您還應該operations與 位于同一列op,因此:
basicOp :: Char -> Int -> Int -> Int
basicOp x y z = (operations !! (op x)) y z
where op x = head $ findIndices (== x) " -*/" :: Int
operations = [( ), (-), (*), (div)] :: [(Int -> Int -> Int)]
但是型別不是必需的,您可以將其簡化為:
basicOp :: Char -> Int -> Int -> Int
basicOp x = (operations !! op x)
where op x = head $ findIndices (== x) " -*/"
operations = [( ), (-), (*), (div)]
并與lookup :: Eq a => a -> [(a, b)] -> Maybe b:
basicOp :: Char -> Int -> Int -> Int
basicOp x | Just y <- lookup x operations = y
| otherwise = …
where operations = [(' ', ( )), ('-', (-)), ('*', (*)), ('/', div)]
where…是一個運算式,如果找不到鍵,則計算該運算式
uj5u.com熱心網友回復:
請注意,您可以在單獨的行上為函式本身提供型別
basicOp :: Char -> Int -> Int -> Int
basicOp x y z = (operations !! (op x)) y z
where
op :: Char -> Int
op x = head $ findIndices (== x) " -*/"
operations = [( ), (-), (*), (div)] :: [(Int -> Int -> Int)]
或通過提供顯式 lambda 運算式作為op.
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)]
uj5u.com熱心網友回復:
哦,洗掉op和operations作業的型別宣告。
我認為有時將型別宣告“行內”是可以的(甚至有幫助)。
basicOp :: Char -> Int -> Int -> Int
basicOp x y z = (operations !! (op x)) y z
where
op x = head $ findIndices (== x) " -*/"
operations = [( ), (-), (*), (div)]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/462852.html
