简体   繁体   English

将字符串中的每个其他字母都大写-列表的取/放与首/尾

[英]Capitalize Every Other Letter in a String — take / drop versus head / tail for Lists

I have spent the past afternoon or two poking at my computer as if I had never seen one before. 在过去的一两个下午里,我一直在电脑上戳戳,好像以前从未见过。 Today's topic Lists 今天的话题清单


The exercise is to take a string and capitalize every other letter . 练习是取一个字符串并大写其他字母 I did not get very far... 我没有走很远...

Let's take a list x = String.toList "abcde" and try to analyze it. 让我们以列表x = String.toList "abcde"进行分析。 If we add the results of take 1 and drop 1 we get back the original list 如果我们将take 1的结果与drop 1的结果相加,则会返回原始列表

> x = String.toList "abcde"
['a','b','c','d','e'] : List Char
> (List.take 1 x) ++ (List.drop 1 x)
['a','b','c','d','e'] : List Char

I thought head and tail did the same thing, but I get a big error message: 我以为headtail做了同样的事情,但是却收到一条错误消息:

> [List.head x] ++ (List.tail x)
==================================== ERRORS ====================================

-- TYPE MISMATCH --------------------------------------------- repl-temp-000.elm

The right argument of (++) is causing a type mismatch.

7│   [List.head x] ++ (List.tail x)
                       ^^^^^^^^^^^
(++) is expecting the right argument to be a:

    List (Maybe Char)

But the right argument is:

    Maybe (List Char)

Hint: I always figure out the type of the left argument first and if it is
acceptable on its own, I assume it is "correct" in subsequent checks. So the
problem may actually be in how the left and right arguments interact.

The error message tells me a lot of what's wrong. 错误消息告诉我很多错误。 Not 100% sure how I would fix it. 并非100%知道我将如何解决。 The list joining operator ++ is expecting [Maybe Char] and instead got Maybe [Char] 加入运算符++的列表期望[Maybe Char] ,而是得到Maybe [Char]


Let's just try to capitalize the first letter in a string (which is less cool, but actually realistic). 让我们尝试大写字符串中的第一个字母(虽然不太酷,但实际上是现实的)。

[String.toUpper ( List.head  x)] ++  (List.drop 1 x)

This is wrong since Char.toUpper requires String and instead List.head x is a Maybe Char . 这是错误的,因为Char.toUpper需要String ,而List.head xMaybe Char

[Char.toUpper ( List.head  x)] ++  (List.drop 1 x)

This also wrong since Char.toUpper requires Char instead of Maybe Char . 这也是错误的,因为Char.toUpper需要Char而不是Maybe Char

In real life a user could break a script like this by typing non-Unicode character (like an emoji). 在现实生活中,用户可以通过键入非Unicode字符(如表情符号)来破坏此类脚本。 So maybe Elm's feedback is right. 因此,榆树的反馈也许是正确的。 This should be an easy problem it takes "abcde" and turns into "AbCdE" (or possibly "aBcDe"). 这应该是一个简单的问题,它需要使用“ abcde”并变成“ AbCdE”(或可能的“ aBcDe”)。 How to handle errors properly? 如何正确处理错误?

In Elm, List.head and List.tail both return they Maybe type because either function could be passed an invalid value; 在Elm中, List.headList.tail都返回它们Maybe类型的,因为任何一个函数都可以传递无效的值。 specifically, the empty list. 具体来说就是空列表。 Some languages, like Haskell, throw an error when passing an empty list to head or tail , but Elm tries to eliminate as many runtime errors as possible. 某些语言(例如Haskell)在将空列表传递到headtail时会引发错误,但是Elm尝试消除尽可能多的运行时错误。

Because of this, you must explicitly handle the exceptional case of the empty list if you choose to use head or tail . 因此,如果选择使用headtail ,则必须显式处理空列表的例外情况。

Note: There are probably better ways to achieve your end goal of string mixed capitalization, but I'll focus on the head and tail issue because it's a good learning tool. 注:可能有更好的方法来实现字符串大小写混合的你的最终目标,但我会集中在headtail的问题,因为这是一个很好的学习工具。

Since you're using the concatenation operator, ++ , you'll need a List for both arguments, so it's safe to say you could create a couple functions that handle the Maybe return values and translate them to an empty list, which would allow you to use your concatenation operator. 由于您使用的是串联运算符++ ,因此两个参数都需要一个List,因此可以肯定地说您可以创建几个函数来处理Maybe返回值并将它们转换为空列表,这将允许您可以使用串联运算符。

myHead list =
  case List.head list of
    Just h -> [h]
    Nothing -> []

myTail list =
  case List.tail list of
    Just t -> t
    Nothing -> []

Using the case statements above, you can handle all possible outcomes and map them to something usable for your circumstances. 使用上面的case陈述,您可以处理所有可能的结果并将它们映射到适合您的情况的事物。 Now you can swap myHead and myTail into your code and you should be all set. 现在您可以将myHeadmyTail交换到您的代码中,您应该已经myTail

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM