function - Int and Num type of haskell -


i have below code take args set offset time.

setoffsettime :: (ord a, num b)=>[a] -> b setoffsettime [] = 200 setoffsettime (x:xs) = read x::int 

but compiler says "could not deduce (b ~ int) context (ord a, num b) bound type signature setoffsettime :: (ord a, num b) => [a] -> b

also found not use 200.0 if want float default value. compilers says "could not deduce (fractional b) arising literal `200.0'"

could 1 show me code function (not in prelude) takes arg store variable can use in other function? can in main = do, hope use elegant function achieve this. there global constant stuff in hasekll? googled it, seems not.

i wanna use haskell replace of python script although not easy.

i think type signature doesn't quite mean think does:

setoffsettime :: (ord a, num b)=>[a] -> b

what says "if give me value of type [a], type a you choose member of ord type class, give value of type b, type b you choose member of num type class". caller gets pick particular types a , b used each time setoffsettime called.

so trying return value of type int (or float, or particular type) doesn't make sense. int indeed member of type class num, it's not any member of type class num. according type signature, should able make brand new instance of num you've never seen before, import setoffsettime module, , call value of new type.

to come acceptable return value, can only use functions likewise return arbitrary num. can't use functions of particular concrete types.

existential types mechanism allowing callee choose value type variable (and caller has written work regardless of type is), that's not want getting while you're still learning.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -