gpt4 book ai didi

haskell - 忽略函数的第二个参数

转载 作者:行者123 更新时间:2023-12-02 10:36:15 25 4
gpt4 key购买 nike

假设我有这个代码:

import qualified Data.Map as Map
import qualified Data.Set as Set

let s = Set.fromList [0,1,2]
m = Map.fromList [(1,"a"),(2,"b"),(3,"c")]
in Map.filterWithKey (\k _ -> Set.member k s) m

我想摆脱 lambda 表达式:

Map.filterWithKey (flip Set.member s . const) m

但这不能编译。我不明白为什么......请帮忙。

Couldn't match type ‘Bool’ with ‘[Char] -> Bool’
Expected type: (b0 -> a) -> Set.Set (b0 -> a) -> [Char] -> Bool
Actual type: (b0 -> a) -> Set.Set (b0 -> a) -> Bool
In the first argument of ‘flip’, namely ‘Set.member’
In the first argument of ‘(.)’, namely ‘flip Set.member s’

最佳答案

  \k _ -> Set.member k s
≡ \k -> \_ -> Set.member k s
≡ \k -> const $ Set.member k s
≡ \k -> const $ flip Set.member s k
≡ const . flip Set.member s
≡ const . (`Set.member`s)

我不确定是什么让你认为组合应该是相反的,但这并没有多大意义 - 毕竟,const 返回一个 (常量)函数,但 Set.member 不能在其任何参数中使用函数。

关于haskell - 忽略函数的第二个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33423159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com