【问题标题】:Haskell: Map and Zip A ListHaskell:映射和压缩列表
【发布时间】:2017-11-02 06:06:15
【问题描述】:

好吧,看来我对 Haskell 很不满意,我发誓我一直被所有这些问题所困扰。无论如何,我需要取两个列表,压缩它们,然后用总和映射。所以这是我到目前为止所拥有的,但它不起作用:

zipMapList :: [a] -> [a] -> [a]
zipMapList x y = do
    let zipList = zip x y
    let mapList = map + zipList
    mapList

好的,这是我尝试加载文件时遇到的错误:

HW2.hs:18:25: error:
• Couldn't match expected type ‘(a1 -> b) -> [a1] -> [b]’
              with actual type ‘[(a, a)]’
• In the second argument of ‘(+)’, namely ‘zipList’
  In the expression: map + zipList
  In an equation for ‘mapList’: mapList = map + zipList
• Relevant bindings include
    mapList :: (a1 -> b) -> [a1] -> [b] (bound at HW2.hs:18:9)
    zipList :: [(a, a)] (bound at HW2.hs:17:9)
    y :: [a] (bound at HW2.hs:16:14)
    x :: [a] (bound at HW2.hs:16:12)
    zipMapList :: [a] -> [a] -> [a] (bound at HW2.hs:16:1)
    18 |     let mapList = map + zipList    |                             
HW2.hs:19:5: error:
• Couldn't match expected type ‘[a]’
              with actual type ‘(a0 -> b0) -> [a0] -> [b0]’
• Probable cause: ‘mapList’ is applied to too few arguments
  In a stmt of a 'do' block: mapList
  In the expression:
    do let zipList = zip x y
       let mapList = map + zipList
       mapList
  In an equation for ‘zipMapList’:
      zipMapList x y
        = do let zipList = ...
             let mapList = ...
             mapList
• Relevant bindings include
    zipList :: [(a, a)] (bound at HW2.hs:17:9)
    y :: [a] (bound at HW2.hs:16:14)
    x :: [a] (bound at HW2.hs:16:12)
    zipMapList :: [a] -> [a] -> [a] (bound at HW2.hs:16:1)
19 |     mapList    |     ^^^^^^^

我只是不明白错误的含义,我的意思是我得到了一个列表 [a] 和另一个列表 [a],然后我压缩它们并映射然后得到另一个列表,这对我来说没有意义为什么我收到元组错误,有人可以帮忙吗?

【问题讨论】:

  • "然后用总和映射" - 这是什么意思?你能添加一个示例输入和输出吗?
  • [1, 2, 3] [4, 5, 6] 被压缩到 [(1, 4), (2, 5), (3,6)],然后被映射到 [ 5、7、9]。

标签: list haskell zip syntax-error tuples


【解决方案1】:

map + zipList 尝试将mapzipList 相加,如x+y。在那里,您想将+ 作为函数传递,所以它应该是map (+) zipList

但是等等。 zipList 是对的列表,(+) 不接受对作为输入:

-- simplified types for clarity
(+) :: Int -> Int -> Int
-- but we want
add :: (Int, Int) -> Int

一个可能的解决方案是定义我们自己的add

add (x,y) = x+y
-- ...
map add zipList

或使用 lambda

map (\ (x,y) -> x+y) zipList

或取消+:

map (uncurry (+)) zipList

还要注意你的类型是错误的,你需要a是一个数字类型来求和。

zipMapList :: Num a => [a] -> [a] -> [a]
zipMapList xs ys = map (uncurry (+)) (zip xs yz)
-- or, exploiting zipWith
zipMapList xs ys = zipWith (+) xs yz

【讨论】:

    【解决方案2】:

    map 是一个类型为map :: (a -> b) -> [a] -> [b] 的函数,而zip 的类型为zip :: [a] -> [b] -> [(a, b)]。当您编写map + zipList 时,您正在尝试将(a -> b) -> [a] -> [b] 类型的元素添加到[(a,b)] 类型的元素中——您知道这是怎么回事吗?

    至于你如何实现你想要的,这将是算法:

    1. 编写一个匿名函数或命名函数来对压缩元组的元素求和。请注意,(+) 函数的类型是什么 - 它不适用于元组,而是两个不同的输入。因此,您需要先将元组分成 2 个输入,然后才能进行加法
    2. 使用上述函数将map 应用于压缩列表。

    【讨论】:

    • 这是我在使用 haskell 时遇到的问题,我不明白它是如何工作的,因为我不应该能够获取一个列表和另一个列表,然后做一些事情来他们然后返回一个列表?在我的脑海中,我认为它是这样工作的,所以当我看到这些错误时,我就是不明白为什么它不允许我这样做
    • 查看类型签名。如果不出意外,它们看起来是否相同的“形状”?这应该是一个关于你是否可以以同样的方式与他们一起推理的快速赠品
    猜你喜欢
    • 2018-10-13
    • 1970-01-01
    • 2012-09-27
    • 2016-10-18
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多