Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Variable not in scope

Tags:

haskell

I have a code as:

main = interact $ show . maxsubseq . map read . words

maxsubseq :: (Ord a,Num a) => [a] -> (a,[a])
maxsubseq = snd . foldl f ((0,[]),(0,[])) where 
f ((h1,h2),sofar) x = (a,b) where
a = max (0,[]) (h1 + x ,h2 ++ [x]) 
b = max sofar a

But I am getting error:

maxSub.hs:6:17: error: Variable not in scope: h1

maxSub.hs:6:22: error: Variable not in scope: x

maxSub.hs:6:25: error: Variable not in scope: h2 :: [t1]

maxSub.hs:6:32: error: Variable not in scope: x

maxSub.hs:7:9: error: Variable not in scope: sofar :: (t, [t1])

Not able to figure out why??

Any ideas??

Thanks.

like image 878
Zubin Kadva Avatar asked Sep 09 '16 04:09

Zubin Kadva


People also ask

What does variable not in scope mean?

2. "Not in scope" means you are trying to use a name which is not defined in the place in which you are trying to use it. In this case, it happens because you left out a comma after [1..x] , and so your definition of a within the list comprehension doesn't work as it should.

Does Haskell have variables?

So, YES Haskell has true variables. But it does not use mutable variables by default.


1 Answers

main = interact $ show . maxsubseq . map read . words

maxsubseq :: (Ord a,Num a) => [a] -> (a,[a])
maxsubseq = snd . foldl f ((0,[]),(0,[])) where
f ((h1,h2),sofar) x = (a,b) where
    a = max (0,[]) (h1 + x ,h2 ++ [x])
    b = max sofar a

Formats really matter in Haskell...

Perhaps this looks better:

main = interact $ show . maxsubseq . map read . words

maxsubseq :: (Ord a,Num a) => [a] -> (a,[a])
maxsubseq = snd . foldl f ((0,[]),(0,[])) where
    f ((h1,h2),sofar) x = (a,b)
        where {
        a = max (0,[]) (h1 + x ,h2 ++ [x]);
        b = max sofar a;
              }
like image 148
Frederick Zhang Avatar answered Nov 08 '22 03:11

Frederick Zhang