Я читаю Real World на Haskell, пытаюсь решить Ch3, Q10 , используя ghc online .
Пока у меня есть следующий код:
data Direction point = MyLeft point | MyRight point | Straight deriving (Show)
getDirectionFromTriple :: Direction p -> Direction p -> Direction p -> Direction p
getDirectionFromTriple p1 p2 p3
| (length . filter (== MyLeft) [p1, p2, p3]) > 1 = MyLeft p3
| (length . filter (== MyRight) [p1, p2, p3]) > 1 = MyRight p3
| otherwise = Straight
Я получаю следующую ошибку при попытке скомпилировать этот код (только отправленные части, одна и та же ошибка появляется несколько раз):
[1 of 1] Compiling Main ( jdoodle.hs, jdoodle.o )
jdoodle.hs:17:15: error:
* Couldn't match expected type `a0 -> t0 a1'
with actual type `[point0 -> Direction point0]'
* Possible cause: `filter' is applied to too many arguments
In the second argument of `(.)', namely
`filter (== MyLeft) [p1, p2, p3]'
In the first argument of `(>)', namely
`(length . filter (== MyLeft) [p1, p2, p3])'
In the expression: (length . filter (== MyLeft) [p1, p2, p3]) > 2
|
17 | | (length . filter (== MyLeft) [p1, p2, p3]) > 2 = MyLeft p3
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
jdoodle.hs:17:35: error:
* Couldn't match expected type `point0 -> Direction point0'
with actual type `Direction p'
* In the expression: p1
In the second argument of `filter', namely `[p1, p2, p3]'
In the second argument of `(.)', namely
`filter (== MyLeft) [p1, p2, p3]'
* Relevant bindings include
p3 :: Direction p (bound at jdoodle.hs:16:30)
p2 :: Direction p (bound at jdoodle.hs:16:27)
p1 :: Direction p (bound at jdoodle.hs:16:24)
getDirectionFromTriple :: Direction p
-> Direction p -> Direction p -> Direction p
(bound at jdoodle.hs:16:1)
|
17 | | (length . filter (== MyLeft) [p1, p2, p3]) > 2 = MyLeft p3
| ^^
jdoodle.hs:17:39: error:
* Couldn't match expected type `point0 -> Direction point0'
with actual type `Direction p'
* In the expression: p2
In the second argument of `filter', namely `[p1, p2, p3]'
In the second argument of `(.)', namely
`filter (== MyLeft) [p1, p2, p3]'
* Relevant bindings include
p3 :: Direction p (bound at jdoodle.hs:16:30)
p2 :: Direction p (bound at jdoodle.hs:16:27)
p1 :: Direction p (bound at jdoodle.hs:16:24)
getDirectionFromTriple :: Direction p
-> Direction p -> Direction p -> Direction p
(bound at jdoodle.hs:16:1)
|
17 | | (length . filter (== MyLeft) [p1, p2, p3]) > 2 = MyLeft p3
| ^^
Буду признателен за предложения по исправлению кода или за более краткие решения, определяющие доминирующее направление из тройки точек.