首页 > 解决方案 > 为什么我在 haskell 中查找时出现错误?

问题描述

我用 Haskell 中的查找编写了下面的代码:

justCheck ::  a -> b -> Environment -> Bool
justCheck x y env = case lookup x env of
                                    Just a -> find y env
                                    Nothing -> error "Variable not found."

当我尝试运行代码时,在以下几行中出现“Normal”和“Just”错误:

          Just a -> find y env
          Nothing -> error "Variable not found."

方法 find 搜索列表 Environment 以查看 y 是否存在于其中,并且我没有得到任何错误。

数据类型 Environment 定义为 [(String,Int)]

谁能解释为什么我在这些行上出现错误?

编辑:

查找定义为:

find :: Int -> [(String, Int)] -> Bool
find y [] = error "Variable not found"
find y (x:xs) = if y == (snd x) then True else find y xs

标签: haskell

解决方案


推荐阅读