Haskell - 使用toUpper将列表[String]中的所有字母大写 [英] Haskell - Capitalize all letters in a list [String] with toUpper

查看:959
本文介绍了Haskell - 使用toUpper将列表[String]中的所有字母大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表[String]的任务ist来删除列表中的那些元素,它们有q或p,然后用toUpper大写列表中的所有字母。

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.

我尝试过的如下:

delAndUpper :: [String] -> [String]
delAndUpper myList = filter (\x -> not('p' `elem` x || 'q' `elem` x)) myList

它正确地从列表中删除不需要的元素,但是我无法应用于此列表上的上一个,因为toUpper的类型是Char。

It removes the unwanted elements from the list properly, however I can't apply toUpper on this list since the type of toUpper is Char.

我尝试使用地图,它不起作用。

I tried it with map and it does not work.

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

我知道,这行代码中的toUpper获取了一个列表作为值,因此它无法正常工作,但是知道如何将级别降到列表中,并将应用映射到上层

I know, that toUpper in this line of code gets a list as value and therefore it can't work, but know how to go a level down into the list and the apply map toUpper.

你能帮助我吗?

提前感谢

问候语

推荐答案

更深层次的映射



您需要使用 map(map toUpper)

这是因为你有 [String] 代替的 String

toUpper           :: Char -> Char
map toUpper       :: [Char] -> [Char]

ie

map toUpper       :: String -> String
map (map toUpper) :: [String] -> [String]

映射toUpper 大写字符串,通过使每个字母大写,所以 map(map toUpper)大写字符串列表中的每个字符串。

map toUpper capitalises a String, by making each letter uppercase, so map (map toUpper) capitalises each String in a list of Strings.

你的函数变为

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

dave4420做了一个好建议 (map.map)toUpper 是一种简洁的写作方式 map(map toUpper)这可以帮助你很简单而自然地认为两个列表级别 - 看看他的答案。

dave4420 made a good suggestion that (map.map) toUpper is a neat way of writing map (map toUpper) that helps you think two list levels in quite simply and naturally - have a look at his answer too.

你问是否有较短的写入条件的方法,而不喜欢硬编码`q` `p` 。我同意这些多个`elem` 位不漂亮。让我们传递不允许的信件列表,并整理一下:

You asked if there was a shorter way to write the condition, and didn't like hard coding the `q` and `p`. I agree those multiple `elem` bits aren't pretty. Let's pass in the list of disallowed letters and tidy up a bit:

delAndUpper omit strings = map (map toUpper) (filter ok strings) where
            ok xs = not (any (`elem` omit) xs)

这里(`elem` omit)检查一个字符,如果它在列表中,会导致我们忽略这个词,所以(any(` elem` omit)xs)检查是否禁止任何 xs 的字符。当然,如果没有一个是禁止的,它是 ok

Here (`elem` omit) checks a character if it's in the list of ones that would cause us to omit the word, so (any (`elem` omit) xs) checks if any of the characters of xs are forbidden. Of course if none are forbidden, it's ok.

你原来的 delAndUpper 现在将是 delAndUpperpq,或者如果您还要禁止资本P和Q, delAndUpperpqPQ。 (稍后再来)

Your original delAndUpper would now be delAndUpper "pq", or if you also want to disallow capital P and Q, delAndUpper "pqPQ". (More on this later.)

t写下 ok 稍短一点。我的第一个想法是使用 pointfree (请参阅我对另一个问题的回答有关如何使其在ghci中运行的细节),但似乎挂起,所以使用一些标准的转换技巧,我们可以使用一个函数来编写而不是通过执行(而不是)f 而不是 not.f 给我们一个Bool两个参数,就像我们一样一个功能,刚刚给我们一个Bool第一次输入后。 任何正在采取(`elem` omit)作为其第一个参数。这给我们

Let's see if we can't write ok a little shorter. My first thought was to use pointfree on it (see my answer to another question for details of how to get it running in ghci), but it seemed to hang, so using some standard transformation tricks, we can compose not with a function that takes two arguments before giving us a Bool by doing (not.).f instead of not.f as we would with a function which just gave us a Bool after the first input. any is taking (`elem` omit) as its first argument. This gives us

ok xs = ((not.).any) (`elem` omit) xs

从中我们可以删除尾随的xs:

from which we can remove the trailing xs:

ok = ((not.).any) (`elem` omit) 

和inline:

delAndUpper omit strings = map (map toUpper) (filter (((not.).any) (`elem` omit)) strings)

我不热衷于追踪 string 或者:

delAndUpper omit = map (map toUpper).filter (((not.).any) (`elem` omit))

(我们可以摆脱省略参数,并且完全免费,但是在我难以阅读的道路上,我的口味会相当差一点。)

(We could get rid of the omit argument as well and go completely point free, but that would go quite a bit too far down the hard-to-read road for my taste.)

> delAndUpper "pq" $ words "The Queen has probably never had a parking ticket."
["THE","QUEEN","HAS","NEVER","HAD","A","TICKET."]

这是必需的行为吗?仔细排除小写变体,然后使所有大写字母似乎很奇怪。我们可以这样做:

Is this the required behaviour? It seems strange to carefully exclude the lowercase variants and then make everything uppercase. We could do it the other way round:

upperAndDel omit = filter (((not.).any) (`elem` omit)).map (map toUpper)

> upperAndDel "PQ" $ words "The Queen has probably never had a parking ticket."
["THE","HAS","NEVER","HAD","A","TICKET."]

这篇关于Haskell - 使用toUpper将列表[String]中的所有字母大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆