I've been dabbling in Haskell for a few days now and I'm totally hooked onto it. Never have I enjoyed learning a new language this much! I'm still learning and given the nature of the language it looks like I will continue in this state (albeit progressing) for quite some time. Learning & unlearning a few things along the way.
Yesterday, I was stuck on a piece of code and it just refused to compile, the code being:-
My problem here was that I kept getting:module Test where
import List(isPrefixOf);
main = do {
revStr <- getLine;
reverse_str revStr;
}
reverse_str [] = [];
reverse_str (x:xs) = reverse_str (xs) ++ [x];
subst _ _ [] = [];
subst [] _ xs = xs;
subst from to xs@(a:as) =
if from `isPrefixOf` xs
then
to ++ subst from to (drop (length from) xs)
else
a : subst from to as
for "main" and the whole time I kept thinking that the type of "reverse_str" was for some reason wrong! Only a few minutes later did I realize that it was "main" itself that was screwed up. It all was fixed by wrapping the "reverse_str" call in a "return" whose type is "(Monad m) => a -> m a".Couldn't match expected type `IO' against inferred type `[]'
Expected type: IO t
Inferred type: [Char]
Baby steps perhaps, but this has gone a long way in teaching me about Haskell's type inference & strictness!
P.S. - Obviously you don't need something like reverse_str as the Prelude already provides a reverse!

2 comments:
Hi there! Glad you're enjoying learning Haskell. =) I just thought I would drop a comment to invite you to the #haskell IRC channel on freenode.net -- it's full of friendly people who like answering questions (imagine that!) and is a really great place to hang out if you're learning the language.
Thanks for the tip Brent!
Post a Comment