[Haskell-cafe] Why is Haskell flagging this?
David Leimbach
leimy2k at gmail.com
Sat Dec 18 01:45:31 CET 2010
No problem. Haskell is a different animal than even other functional
languages in my experience, and it takes time to get used to the coolness in
the type system, the lazy evaluation, the point free style, functional
composition and all the other interesting techniques you now have at your
fingertips for writing very expressive code :-).
Do that for a while then go back to algol based languages, and wonder why
the heck anyone uses those on purpose :-). (yeah there's good reasons to
use them, but it starts to feel confining)
Dave
On Fri, Dec 17, 2010 at 4:28 PM, michael rice <nowgate at yahoo.com> wrote:
> Hi, all.
>
> Plenty of answers. Thank you.
>
> Putting the list in the IO monad was deliberate. Another one I was looking
> at was
>
> f :: String -> IO String
> f s = do return s
>
> main = do ios <- f "hello"
> fmap tail ios
>
> which worked fine
>
> So, the big error was trying to add 1 + [1,2,3,4,5].
>
> I considered that I needed an additional fmap and thought I had tried
>
> fmap (fmap (1+)) iol
>
> but must have messed it up, because I got an error. I guess I was on the
> right track.
>
> I like to try various combinations to test my understanding. It's kind of
> embarrassing when I get stumped by something simple like this, but that's
> how one learns.
>
> Thanks again,
>
> Michael
>
> --- On Fri, 12/17/10, Daniel Fischer <daniel.is.fischer at googlemail.com>
> wrote:
>
>
> From: Daniel Fischer <daniel.is.fischer at googlemail.com>
> Subject: Re: [Haskell-cafe] Why is Haskell flagging this?
> To: haskell-cafe at haskell.org
> Cc: "michael rice" <nowgate at yahoo.com>
> Date: Friday, December 17, 2010, 4:24 PM
>
>
> On Friday 17 December 2010 18:04:20, michael rice wrote:
> > I don't understand this error message. Haskell appears not to
> understand
> > that 1 is a Num.
> >
> > Prelude> :t 1
> > 1 :: (Num t) => t
> > Prelude> :t [1,2,3,4,5]
> > [1,2,3,4,5] :: (Num t) => [t]
> > Prelude>
> >
> > Michael
> >
> > ===================
> >
> > f :: [Int] -> IO [Int]
> > f lst = do return lst
> >
> > main = do let lst = f [1,2,3,4,5]
> > fmap (+1) lst
>
> The fmap is relative to IO, your code is equivalent to
>
> do let lst = (return [1,2,3,4,5])
> fmap (+1) lst
>
> ~>
>
> fmap (+1) (return [1,2,3,4,5])
>
> ~>
>
> do lst <- return [1,2,3,4,5]
> return $ (+1) lst
>
> but there's no instance Num [Int] in scope
>
> You probably meant
>
>
> do let lst = f [1,2,3,4,5]
> fmap (map (+1)) lst
>
>
> >
> > ===============================
> >
> > Prelude> :l test
> > [1 of 1] Compiling Main ( test.hs, interpreted )
> >
> > test.hs:5:17:
> > No instance for (Num [Int])
> > arising from the literal `1' at test.hs:5:17
> > Possible fix: add an instance declaration for (Num [Int])
> > In the second argument of `(+)', namely `1'
> > In the first argument of `fmap', namely `(+ 1)'
> > In the expression: fmap (+ 1) lst
> > Failed, modules loaded: none.
> > Prelude>
>
>
> --- On *Fri, 12/17/10, Daniel Fischer <daniel.is.fischer at googlemail.com>*wrote:
>
>
> From: Daniel Fischer <daniel.is.fischer at googlemail.com>
> Subject: Re: [Haskell-cafe] Why is Haskell flagging this?
> To: haskell-cafe at haskell.org
> Cc: "michael rice" <nowgate at yahoo.com>
> Date: Friday, December 17, 2010, 4:24 PM
>
> On Friday 17 December 2010 18:04:20, michael rice wrote:
> > I don't understand this error message. Haskell appears not to understand
> > that 1 is a Num.
> >
> > Prelude> :t 1
> > 1 :: (Num t) => t
> > Prelude> :t [1,2,3,4,5]
> > [1,2,3,4,5] :: (Num t) => [t]
> > Prelude>
> >
> > Michael
> >
> > ===================
> >
> > f :: [Int] -> IO [Int]
> > f lst = do return lst
> >
> > main = do let lst = f [1,2,3,4,5]
> > fmap (+1) lst
>
> The fmap is relative to IO, your code is equivalent to
>
> do let lst = (return [1,2,3,4,5])
> fmap (+1) lst
>
> ~>
>
> fmap (+1) (return [1,2,3,4,5])
>
> ~>
>
> do lst <- return [1,2,3,4,5]
> return $ (+1) lst
>
> but there's no instance Num [Int] in scope
>
> You probably meant
>
>
> do let lst = f [1,2,3,4,5]
> fmap (map (+1)) lst
>
>
> >
> > ===============================
> >
> > Prelude> :l test
> > [1 of 1] Compiling Main ( test.hs, interpreted )
> >
> > test.hs:5:17:
> > No instance for (Num [Int])
> > arising from the literal `1' at test.hs:5:17
> > Possible fix: add an instance declaration for (Num [Int])
> > In the second argument of `(+)', namely `1'
> > In the first argument of `fmap', namely `(+ 1)'
> > In the expression: fmap (+ 1) lst
> > Failed, modules loaded: none.
> > Prelude>
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20101217/ae0d72b1/attachment.htm>
More information about the Haskell-Cafe
mailing list