Ignore function second argument

Let's say I have this code:

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

I wanted to get rid of the lambda expression:

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

but it does not compile. and I can’t understand why .. Please help.

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’
+4
source share
1 answer
  \k _ -> Set.member k s
≡ \k -> \_ -> Set.member k s
≡ \k -> const $ Set.member k s
≡ \k -> const $ flip Set.member s kconst . flip Set.member sconst . (`Set.member`s)

I'm not sure what made you think that the composition should be the other way around, but in fact it does not make much sense. ultimately constreturns a function (constant), but Set.membercannot use the function in any of its arguments.

+4
source

Source: https://habr.com/ru/post/1613716/


All Articles