我花了过去的一两个下午或两个戳到我的电脑上,就好像我以前从未见过一样。今天的话题列表将字符串中的每一个其他字母都大写 - 列表中的每个其他字母大写
的练习是一个字符串并将利用其他字母。我没有走得太远......
让我们列出x = String.toList "abcde"
并尝试分析它。如果我们加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
我想head
和tail
做同样的事情,但我得到一个很大的错误消息:
> [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.
错误信息告诉我很多问题。不是100%确定我会如何解决它。加盟运营商++
名单期待[Maybe Char]
,而是得到Maybe [Char]
让我们只是试图利用在字符串中的第一个字母(这是不太爽,但实际上现实的)。
[String.toUpper (List.head x)] ++ (List.drop 1 x)
这是错误的,因为Char.toUpper
需要String
,而是List.head x
是Maybe Char
。
[Char.toUpper (List.head x)] ++ (List.drop 1 x)
这也错了,因为Char.toUpper
需要Char
,而不是Maybe Char
。
在现实生活中,用户可以通过键入非Unicode字符(如表情符号)来打破脚本。所以也许榆树的反馈是正确的。这应该是一个简单的问题,它需要“abcde”并变成“AbCdE”(或可能是“aBcDe”)。如何正确处理错误?
- JavaScript中的同一个问题:How do I make the first letter of a string uppercase in JavaScript?