0

So the Function im writing is supposed to take in some amount of Points (punkte) and Bonus Points and then compair it to the punkte list and return the corresponding grade (noten list).

Im fairly new to haskell and bad at interpreting errors.

I get that Infinite type error fairly often i never really know why. Can you please explain that to me ? And maybe give a solution?

Function:

import Data.Char -- i know i don't need this so 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

-- this constructes an 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 this is the Error i get

blatt1.hs:17:48: error:
• Occurs check: cannot construct the infinite type: a ~ [a]
• In the second argument of ‘elemIndex’, namely ‘p’
  In the first argument of ‘fromJust’, namely ‘(elemIndex x p)’
  In the second argument of ‘(!!)’, namely
    ‘(fromJust (elemIndex x p))’
• Relevant bindings include
    xs :: [a] (bound at blatt1.hs:16:20)
    x :: a (bound at blatt1.hs:16:18)
    p :: a (bound at blatt1.hs:16:15)
    berechneNote2 :: a -> [a] -> Double (bound at blatt1.hs:16:1)
3
  • Why do you use elemIndex here? Commented Jul 28, 2017 at 14:29
  • what should i use instead? Commented Jul 28, 2017 at 14:33
  • You may want to add type annotations for your functions. That should help you have a good mental model of what you're doing and can lead to better error messages if ghc infers something at odds with what you've declared you wanted. Commented Jul 28, 2017 at 14:54

1 Answer 1

2
| p == x = noten !! (fromJust (elemIndex x p))

From p == x, p and x are of the same type. Let's name this a.

From elemIndex x p, p must be of a list type, say [b]. Also x must be a potential element of p, so it must have type b.

So, we get a ~ [b] ~ [a], which is nonsense: a list can not contain elements of the same list type.

Also, we strongly recommend to provide the intended type for every top-level definition. In that way, GHC will produce better type errors, pointing out the differences between what type we intend to define, and the type which instead results from our code.

Sign up to request clarification or add additional context in comments.

1 Comment

i would have sat there staring at this for the next ours THX for the quick answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.