未定义的变量,Haskell

Mac*_*iek 5 haskell

我有一个简单的问题.哈斯克尔正在向57 - Undefined variable "f" error我投掷,我不知道为什么.如果你能看一下,我会很感激的.

码:

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
Run Code Online (Sandbox Code Playgroud)

类型定义:

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

sep*_*p2k 9

where块仅适用于它之前的情况,而不适用于eval'函数的所有情况.因此f定义(但未使用)eval' (Id id) = 5,但它不在第57行的范围内.要修复此问题,您需要在第57行之后直接移动where块.