【问题标题】:Haskell -- mapping function to permutationHaskell——映射函数到排列
【发布时间】:2013-08-30 21:15:06
【问题描述】:

我定义函数 listToNumber 如下:

listToNumber = foldl1 (\acc xs -> acc*10 + xs)

只提供一个数字列表时,它可以正常工作,例如:

listToNumber [1,2,3,4] = 1234
map listToNumber [[1,2,3,4], [5,4,3,2]] = [1234,5432]

但是,以下返回错误消息:

map listToNumber permutations[1..3]

谁能解释一下?

附:错误信息如下:

Couldn't match expected type `[t1] -> t0' with actual type `[b0]'
The function `map' is applied to three arguments,
but its type `([b0] -> b0) -> [[b0]] -> [b0]' has only two
In the expression: map listToNumber permutations [1 .. 3]
In an equation for `it':
    it = map listToNumber permutations [1 .. 3]

【问题讨论】:

    标签: haskell map permutation


    【解决方案1】:

    试试map listToNumber (permutations [1 .. 3])

    在 ghci 中,您可以使用 :t 检查函数或表达式的类型

    > :t map
    > map :: (a -> b) -> [a] -> [b]
    

    im 表示map 需要一个函数和一个列表并返回一个列表,但在map listToNumber permutations [1 .. 3] 中,您尝试将两个函数和一个列表传递给映射 (因为函数应用程序关联到左侧)。

    【讨论】:

      【解决方案2】:

      另一个变种:map listToNumber $ permutations [1..3]。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-27
        • 2020-05-16
        • 1970-01-01
        • 2019-10-12
        • 1970-01-01
        • 2017-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多