Haskell - Capitalize all letters in a list [String] with toUpper

前端 未结 3 909
陌清茗
陌清茗 2021-01-14 17:04

I have a list [String] the task ist to remove those elements in the list, which have \"q\" or \"p\" and then capitalize all letters in the list with toUpper.

What I

3条回答
  •  清酒与你
    2021-01-14 17:41

    You're almost there, you just need a second map.

    map (map toUpper) (filter (\x -> not('p' `elem` x || 'q' `elem` x)) myList)
    

    This is because String is completely synonymous with [Char] in vanilla haskell. Since the type of map is

    (a->b) -> [a] -> b
    

    and we have

    toUpper :: Char -> Char
    String  :: [Char]
    

    We'll get back another String, except capitalized.

    By the way, that ugly-ish filter can be replaced made prettier with by making it use arrows :) (Think of these like more structured functions)

    map (map toUpper) . filter $ elem 'p' &&& elem 'q' >>> arr (not . uncurry (||))
    

    Gratuitousness? Maybe, but kinda cool.

提交回复
热议问题