y :: (f->f) -> f
y f = f (y f)
indexOf :: Char -> String -> Maybe Int
indexOf c l =
y(\ ff c (h:t) pos ->
if (c == h) then Just pos
else ff c t (pos + 1)
)(c, l, 0)
indexOf c [] = Nothing
Это дает мне две ошибки компиляции: Код должен найти позицию char c в строке. Например: IndexOf ('а', 'лампа') = 2Haskell. Рекурсивная Лямбда. Ошибки
Couldn't match expected type `[(Char, String, Integer)]
-> Integer -> Maybe Integer'
with actual type `Maybe Int'
The lambda expression `\ ff c (h : t) pos -> ...'
has four arguments,
but its type `((Char, String, Integer) -> Maybe Int)
-> (Char, String, Integer) -> Maybe Int'
has only two
In the first argument of `y', namely
`(\ ff c (h : t) pos
-> if (c == h) then Just pos else ff c t (pos + 1))'
In the expression:
y (\ ff c (h : t) pos
-> if (c == h) then Just pos else ff c t (pos + 1))
(c, l, 0)
Couldn't match expected type `[(Char, String, Integer)]
-> Integer -> Maybe Integer'
with actual type `Maybe Int'
The function `ff' is applied to three arguments,
but its type `(Char, String, Integer) -> Maybe Int' has only one
In the expression: ff c t (pos + 1)
In the expression: if (c == h) then Just pos else ff c t (pos + 1)
Я не знаю, как восстановить его. Пожалуйста, дайте мне руку :)
Функции Haskell называются 'function abc ...' not 'function (a, b, c, ...)'. Структура '(a, b, c, ...)' называется кортежем, но определенная функция не принимает ее. – AJFarmar