Faiza Fthm
Faiza Fthm

Reputation: 1

Haskell is throwing the 57 - Undefined variable "f" error at me

I've got a quick question. Haskell is throwing the 57 - Undefined variable "f" error at me and I've no idea why. I'd appreciate it if you could have a look at this. Code:

eval :: Expr -> Environment -> Float
eval expr env = eval' expr
    where
    eval' :: Expr-> Float
    eval' (Num num) = num
    eval' (App app exprs) = foldl1 (f) (map eval' exprs) -- **Line 57**
    eval' (Id id) = 5
        where
        f = getFunctionForApp app                    -- **f is here** 
        getFunctionForApp :: String -> (Float->Float->Float)
        getFunctionForApp "+" = (+)
        getFunctionForApp "-" = (-)
        getFunctionForApp "*" = (*)
        getFunctionForApp "/" = (/)
        getIdVal :: String -> Environment -> Float
        getIdVal id ((curId, val):envrs) 
            |curId == id = val
            | otherwise = getIdVal id envrs

Type definition:

data Expr = Num Float | Id String | App String [ Expr ]
           deriving (Eq, Ord, Show)
type Environment = [ ( String, Float ) ]

Upvotes: 0

Views: 280

Answers (2)

pigworker
pigworker

Reputation: 43393

The problem is that the inner where clause attaches to the line

eval' (Id id) = 5

but it's needed for the line above

eval' (App app exprs) = foldl1 (f) (map eval' exprs) -- **Line 57**

In general, each line of a function definition has its own scope. A where clause may refer to pattern variables from the line it's attached to, and scopes over only the remainder of that line.

Upvotes: 10

phipsgabler
phipsgabler

Reputation: 20960

I can't really say it, but after trying to decrypt the code I guess you meant the following:

eval :: Expr -> Environment -> Float
eval expr env = eval' expr 
    where eval' :: Expr -> Float 
          eval' (Num num) = num 
          eval' (App app exprs) = foldl1 (f) (map eval' exprs)
              where f = getFunctionForApp app -- that has to be in this line
          eval' (Id id) = 5 

(Now with the formatted code I'm sure that's it. where clauses only work for the line immediately before the clause)

Upvotes: 11

Related Questions