Sunday 11 September 2022

.hs course lol :)

A strange quote from learnyouahaskell (note: the course looks very good and well-written; the guy behind it seems super bright; I started reading the stuff and I certainly don't regret it... - looks almost like first fixie ride). The stuff below is a(n almost direct) quote. I modified some coloring, added line breaks and so on, for readability. Let's go:

For instance, the length function has a type declaration of

length :: [a] -> Int 

instead of having a more general type of 

(Num b) => length :: [a] -> b.

I think that's there for historical reasons or something, although in my opinion, it's pretty stupid. Anyway, if we try to get a length of a list and then add it to 3.2, we'll get an error because we tried to add together an Int and a floating point number. So to get around this, we do  

fromIntegral (length [1,2,3,4]) + 3.2 

and it all works out.

My comment, if you allow me... - I can't (as of yet) imagine any context where I need to add a list's length (or len, or Count, or size() etc.) to a floating point value. Seems super-exotic, but perhaps you can think of that... If I ever encounter this kind of need, I'd think of it as more obvious and I'd do that. It's simple. And if I start complaining, or get tired of too many casts etc., perhaps I'd write a workaround with a nice name. 

And name-calling like that (note the precaution though, by casting to opinion type :) seems stupid to me, in this particular context. I have nothing against such approach if something is really super-weird and very difficult to neutralize or adapt in a few lines of code. If it's more complicated, I'd push it further and write dozens lines of code. Sometimes I just get stuck, hence my angry rants describing spectacular failures with Python. But complaining about something so standard?

No comments:

Post a Comment