Graham Bewley
Graham Bewley

Reputation: 195

Haskell error: Variable not in scope

I'm a bit new to Haskell but I've been working this problem for a couple hours with no luck.

I'm trying to implement something similar to a filter, except a predicate and list is passed to a function and it returns a tuple of two lists, one which is filtered by the predicate and one which is not.

divideList :: (a -> Bool) -> [a] -> ([a],[a])
divideList p xs = (f, nf) where 
f = doFilter p xs
nf = doFilter (not . p) xs

doFilter :: (a -> Bool) -> [a] -> [a]
doFilter _ [] = []
doFilter p (x:xs) = [x | p x] ++ doFilter p xs

The second function, doFilter works properly. It applies the filter to its list and spits out the appropriate list. (i.e. If I just use doFilter (>3) [1,2,3,4,5,6] it will work properly)

My issue is with the first function. When I use divideList (>3) [1,2,3,4,5,6] I get a number of Variable not in scope errors. The errors are listed below:

AddListOperations.hs:20:23: error:
    Variable not in scope: p :: a -> Bool

AddListOperations.hs:20:25: error: Variable not in scope: xs :: [a]

AddListOperations.hs:21:31: error:
    Variable not in scope: p :: a -> Bool

AddListOperations.hs:21:34: error: Variable not in scope: xs :: [a]

Like I said, I have only been messing around with Haskell for a few days so let me know if I'm leaving out any important information.

Upvotes: 4

Views: 7930

Answers (2)

Graham Bewley
Graham Bewley

Reputation: 195

Thanks to Alec, I found that all I needed to do was indent the statements underneath where:

divideList :: (a -> Bool) -> [a] -> ([a],[a])
divideList p xs = (f, nf) where 
    f = doFilter p xs
    nf = doFilter (not . p) xs

Upvotes: 0

Zeta
Zeta

Reputation: 105955

Indent both f and nf:

divideList :: (a -> Bool) -> [a] -> ([a],[a])
divideList p xs = (f, nf) where
  f  = doFilter p xs
  nf = doFilter (not . p) xs

After all, where would your where block stop?

By the way, divideList is partition from Data.List.

Upvotes: 2

Related Questions