首页 > 解决方案 > Haskell 不断给我类型错误

问题描述

不知道我做错了什么

type Name = String
type Location = (Float,Float)
type RainfallFigures = [Int]
type Place = (Name,Location,RainfallFigures)

rtnClosestDryPlace :: (Location -> Int -> [Place] -> [(Name,Float)]) -> (Name,Float)
rtnClosestDryPlace p1 n ((a,p2,c):xs) = rtnLowestDistance (distanceList p1 (rtnFullDryPlaces ((a,p2,c):xs) n))

    Couldn't match expected type ‘(Name, Float)’
                      with actual type ‘Int
                    -> [(Name, Location, RainfallFigures)] -> (Name, Float)’
        • The equation(s) for ‘rtnClosestDryPlace’ have three arguments,
          but its type ‘(Location -> Int -> [Place] -> [(Name, Float)])
                        -> (Name, Float)’
          has only one
    rtnClosestDryPlace p1 n ((a,p2,c):xs) = rtnLowestDistance (distanceList p1 (rtnFullDryPlaces ((a,p2,c):xs) n))
       | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



    template.hs:90:73: error:
        • Couldn't match expected type ‘(Float, Float)’
                      with actual type ‘Location -> Int -> [Place] -> [(Name, Float)]’
        • Probable cause: ‘p1’ is applied to too few arguments
          In the first argument of ‘distanceList’, namely ‘p1’
          In the first argument of ‘rtnLowestDistance’, namely
            ‘(distanceList p1 (rtnFullDryPlaces ((a, p2, c) : xs) n))’
          In the expression:
            rtnLowestDistance
              (distanceList p1 (rtnFullDryPlaces ((a, p2, c) : xs) n))
       |
    90 | rtnClosestDryPlace p1 n ((a,p2,c):xs) = rtnLowestDistance (distanceList p1 (rtnFullDryPlaces ((a,p2,c):xs) n))
       |          ^^

Location, Int, [place] 都被发送到返回 [(Name, Float)] 的函数,该函数又被发送到返回 (Name,Float) 的函数 我不知道为什么这个程序无法运行. 为什么不能匹配类型

编辑:重写函数后,我设法正确地写下来,没有匹配错误

标签: haskell

解决方案


你的类型是错误的:

rtnClosestDryPlace :: (Location -> Int -> [Place] -> [(Name,Float)]) -> (Name,Float)

表示这rtnClosestDryPlace是一个函数,其输入参数具有类型

(Location -> Int -> [Place] -> [(Name,Float)]) 

并且其输出参数具有类型

(Name,Float)

因此,该类型承诺,给定一个 3 参数函数,下面的代码能够返回一对。那不是你想要的。

我猜你实际上想要类似的东西

rtnClosestDryPlace :: Location -> Int -> [Place] -> [(Name,Float)]

推荐阅读