나는 재귀 함수 mxAndC
을 작성하고 있습니다. 목록을 줄 때 튜플을 반환해야합니다. 튜플은 첫 번째 요소로 지정된 목록의 최대 값을 가지며 두 번째 요소는 요소가 목록에서 발생한 횟수입니다. 할당으로 도우미 함수를 만들 수 없습니다. 최대 목록 및 발생 횟수를 반환하는 함수
mxAdC "bananas" = (s,1)
mxAdC "banana" =(n,2)
mxAdC [mod x 4 | x <- [1..50]] -> (3,12)
나는 다음과 같은 한 :
mxAdC = go 0
where go count (x:xs)
| count /= 0 = (mx, count)
| null ((x:xs)) = error "The list is empty"
| x == mx = go (count+1) xs
where mx = maximum (x:xs)
을 그리고 오류를 받고 있어요 : I 출력 다음과 같은 기대하고 있습니다
* Ambiguous type variable `a0' arising from a use of `go'
prevents the constraint `(Ord a0)' from being solved.
Relevant bindings include
mxAdC :: [a0] -> (a0, Integer) (bound at hw06.hs:24:1)
Probable fix: use a type annotation to specify what `a0' should be.
These potential instances exist:
instance (Ord a, Ord b) => Ord (Either a b)
-- Defined in `Data.Either'
instance Ord Ordering -- Defined in `GHC.Classes'
instance Ord Integer
-- Defined in `integer-gmp-1.0.0.1:GHC.Integer.Type'
...plus 23 others
...plus 38 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
* In the expression: go 0
In an equation for `mxAdC':
mxAdC
= go 0
where
go count (x : xs)
| count /= 0 = (mx, count)
| null ((x : xs)) = error "The list is empty"
| x == mx = go (count + 1) xs
where
mx = maximum (x : xs)
나는 하스켈에서 아주 새로운 해요. 자비로운 전문가가 밖에 나가서이 초보자를 도와 주겠습니까?
함수에 형식 시그니처를 추가해야합니다. – duplode
가능한 하스켈의 복제본 : "모호한 유형 변수 ... \'Integral t '... \'RealFrac t'..."이 코드에서?] (http://stackoverflow.com)/questions/3642277/haskell-what-is-the-source-of-error-ambiguous-type-variable-integral-t) – duplode
@duplode : 나는 그것이 실제로 중복 적이라고 생각하지 않습니다. 그 다른 질문은 _ 대립 _ 유형을 가지고 있습니다, 이것은 원칙적으로 건전하지만 monomorphism-restriction 문제가 있습니다. 동일한 오류 메시지이지만 완전히 다른 근본적인 문제. – leftaroundabout