haskell:将小写字母更改为字符串列表中的大写字母[重复]

时间:2022-05-09 20:44:16

This question already has an answer here:

这个问题在这里已有答案:

My problem is, I would like to change every lowercase letter of the list ["hello","wHatS", "up?"] into capitals.

我的问题是,我想将列表中的每个小写字母[“你好”,“wHatS”,“up?”]改为大写字母。

map toUpper [x] does not work realy...

map toUpper [x]无法正常工作...

it should return ["HELLO", "WHATS", "UP?"]..

它应该返回[“HELLO”,“WHATS”,“UP?”] ..

1 个解决方案

#1


Take a look at type of toUpper, it's Char -> Char, but you have [[Char]]. It means that you have two layers of list functor here, so you should map it twice.

看一下toUpper的类型,它是Char - > Char,但你有[[Char]]。这意味着你在这里有两层list functor,所以你应该将它映射两次。

For pedagogical reasons we may use map here, like this:

出于教学原因,我们可以在这里使用地图,如下所示:

map (map toUpper) yourList

Parenthesis are important here, we give one argument to map :: (a -> b) -> [a] -> [b] and get another function of type [Char] -> [Char] (just what we need!) because of curring.

括号在这里很重要,我们为map ::(a - > b) - > [a] - > [b]提供一个参数,并获得[Char] - > [Char]类型的另一个函数(正是我们需要的!)因为curring。

Once you learn about functors, you may prefer fmap and <$> for this task:

一旦你了解了仿函数,你可能更喜欢fmap和<$>来完成这项任务:

(toUpper <$>) <$> yourList

#1


Take a look at type of toUpper, it's Char -> Char, but you have [[Char]]. It means that you have two layers of list functor here, so you should map it twice.

看一下toUpper的类型,它是Char - > Char,但你有[[Char]]。这意味着你在这里有两层list functor,所以你应该将它映射两次。

For pedagogical reasons we may use map here, like this:

出于教学原因,我们可以在这里使用地图,如下所示:

map (map toUpper) yourList

Parenthesis are important here, we give one argument to map :: (a -> b) -> [a] -> [b] and get another function of type [Char] -> [Char] (just what we need!) because of curring.

括号在这里很重要,我们为map ::(a - > b) - > [a] - > [b]提供一个参数,并获得[Char] - > [Char]类型的另一个函数(正是我们需要的!)因为curring。

Once you learn about functors, you may prefer fmap and <$> for this task:

一旦你了解了仿函数,你可能更喜欢fmap和<$>来完成这项任务:

(toUpper <$>) <$> yourList