Haskell Non type-variable argument Error -
so function im writing supposed take in amount of points (punkte) , bonus points , compair punkte list , return corresponding grade (noten list).
im new haskell , bad @ interpreting errors.
i infinite type error never know why. can please explain me ? , maybe give solution?
function:
import data.char -- know don't need far import data.list import data.maybe punkte = [50,54..86] noten = [4.0,3.7,3.3,3.0,2.7,2.3,2.0,1.7,1.3,1.0] berechnenote p bp | bp > 20 = error "too many bonuspoints" | bp < 0 = error "you can't give negative points" | p > 100 = error "too many points" | p < 0 = error "you can't give negative points" | otherwise = berechnenote2 (p+bp) punkte -- constructes infinite type aparently ? berechnenote2 p (x:xs) | p == x = noten !! (fromjust (elemindex x p)) | p > x = berechnenote2 p xs | p < x = noten !! (fromjust (elemindex x p))
and error
blatt1.hs:17:48: error: • occurs check: cannot construct infinite type: ~ [a] • in second argument of ‘elemindex’, namely ‘p’ in first argument of ‘fromjust’, namely ‘(elemindex x p)’ in second argument of ‘(!!)’, namely ‘(fromjust (elemindex x p))’ • relevant bindings include xs :: [a] (bound @ blatt1.hs:16:20) x :: (bound @ blatt1.hs:16:18) p :: (bound @ blatt1.hs:16:15) berechnenote2 :: -> [a] -> double (bound @ blatt1.hs:16:1)
| p == x = noten !! (fromjust (elemindex x p))
from p == x
, p
, x
of same type. let's name a
.
from elemindex x p
, p
must of list type, [b]
. x
must potential element of p
, must have type b
.
so, a ~ [b] ~ [a]
, nonsense: list can not contain elements of same list type.
also, recommend provide intended type every top-level definition. in way, ghc produce better type errors, pointing out differences between type intend define, , type instead results our code.
Comments
Post a Comment