Map.lookup - 类型声明错误 [英] Map.lookup - type declaration error

查看:139
本文介绍了Map.lookup - 类型声明错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用代码:

  test2 :: Int  - > Map Int Int  - > Int 
test2键缓存
| Map.member key cache = Map.lookup键缓存
|否则= 0

这里我想检查Map中存在的Int并查找值存在。但我得到错误:

 无法匹配预期类型'Int',实际类型为'Maybe a0'
In Map.lookup'的返回类型
在表达式中:Map.lookup键缓存
在`test2'的等式中:
test2键缓存
|成员键缓存= Map.lookup键缓存
|否则= 0

为什么?我检查了地图中存在的钥匙。如何解决它?



更新



Thans为您的答案,但我的真实代码有点复杂:
$ b $ pre $ Co $ Coord int Coord int Int(b)

计算:: Coord - >地图坐标整数 - >整数
计算坐标缓存
|角落coord = 0
|边坐标= 1
| Map.member coord cache = Map.lookup键缓存
|否则=(计算(move_right coord)缓存)+(计算(move_down coord)缓存)
其中(Coord xy)= coord

我更新了这样的代码:

  calculation :: Coord  - >地图坐标整数 - >整数
计算坐标缓存
|角落coord = 0
|边坐标= 1
| Map.member coord cache = Map.findWithDefault(计算(move_right coord)缓存)+(计算(move_down coord)缓存)coord cache
其中(Coord xy)= coord



但是得到下一个错误:

  problem_15。 hs:21:14:
使用'member'时没有(Ord Coord)
的实例
可能的修正:为(Ord Coord)添加一个实例声明
In表达式:成员协调高速缓存

的模式守护单元中,用于计算的等式:
成员协调高速缓存
在计算方程中:
计算坐标缓存
|角落coord = 0
|边坐标= 1
|成员坐标缓存
= findWithDefault(计算(move_right坐标)缓存)
+(计算(move_down坐标)缓存)坐标缓存
其中
(坐标xy)=坐标

problem_15.hs:21:39:
无法与实际类型为`k0 - >的预期类型'整数'
匹配。地图k0 a0 - >在'(+)'的第一个参数中,即
`findWithDefault(calculation(move_right coord)cache)'
在表达式中:
findWithDefault(计算(move_right coord)缓存)
+(计算(move_down coord)缓存)coord缓存


解决方案

Map.lookup键缓存返回Maybe Int,因此编译错误。

最简单的做法是使用 Map.findWithDefault ,如下所示:

  test2 :: Int  - > Map Int Int  - > Int 
test2 key cache = Map.findWithDefault 0键缓存

如果您要使用 Map.lookup ,您可以执行以下操作:

  test2 :: Int - > Map Int Int  - > Int 
test2 key cache =也许是0 id。 Map.lookup键$ cache


I am working with code:

test2 :: Int -> Map Int Int -> Int
test2 key cache 
      | Map.member key cache = Map.lookup key cache
      | otherwise = 0

Here I want to check the existance of Int in the Map and lookup the value if it exists. But I get error:

 Couldn't match expected type `Int' with actual type `Maybe a0'
    In the return type of a call of `Map.lookup'
    In the expression: Map.lookup key cache
    In an equation for `test2':
        test2 key cache
          | member key cache = Map.lookup key cache
          | otherwise = 0

Why? I have checked existance of key in the Map. How can I fix it?

Updated

Thans for your answers, but my real code is a little bit complex:

data Coord = Coord Int Int deriving (Show)

calculation :: Coord -> Map Coord Integer -> Integer
calculation coord cache
           | corner coord = 0
           | side coord = 1
           | Map.member coord cache = Map.lookup key cache
           | otherwise = (calculation (move_right coord) cache) + (calculation (move_down coord) cache)
           where (Coord x y) = coord

I have updated the code like this:

calculation :: Coord -> Map Coord Integer -> Integer
calculation coord cache
           | corner coord = 0
           | side coord = 1
           | Map.member coord cache = Map.findWithDefault (calculation (move_right coord) cache) + (calculation (move_down coord) cache) coord cache
           where (Coord x y) = coord

But get the next error:

problem_15.hs:21:14:
    No instance for (Ord Coord)
      arising from a use of `member'
    Possible fix: add an instance declaration for (Ord Coord)
    In the expression: member coord cache
    In a stmt of a pattern guard for
                 an equation for `calculation':
        member coord cache
    In an equation for `calculation':
        calculation coord cache
          | corner coord = 0
          | side coord = 1
          | member coord cache
          = findWithDefault (calculation (move_right coord) cache)
          + (calculation (move_down coord) cache) coord cache
          where
              (Coord x y) = coord

problem_15.hs:21:39:
    Couldn't match expected type `Integer'
                with actual type `k0 -> Map k0 a0 -> a0'
    In the return type of a call of `findWithDefault'
    In the first argument of `(+)', namely
      `findWithDefault (calculation (move_right coord) cache)'
    In the expression:
        findWithDefault (calculation (move_right coord) cache)
      + (calculation (move_down coord) cache) coord cache

解决方案

Map.lookup key cache returns a Maybe Int, not an Int, thus the compile error.

The easiest way to do what you want is to use Map.findWithDefault like so:

test2 :: Int -> Map Int Int -> Int
test2 key cache = Map.findWithDefault 0 key cache

If you want to use Map.lookup, you can do the following:

test2 :: Int -> Map Int Int -> Int
test2 key cache = maybe 0 id . Map.lookup key $ cache

这篇关于Map.lookup - 类型声明错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆