【发布时间】:2013-01-28 13:08:08
【问题描述】:
大家好,我想获取一个单词列表并返回一个类似的列表,但是 每次出现连续单词时都会进行以下替换。
一个例子是you 并把它变成u
我得到了以下内容:
hep :: [Word] -> [Word]
type Word = String
现在给我的问题是我正在尝试使用大小写表达式,这样我就不必重复代码,但我收到以下错误
Couldn't match expected type `Char' with actual type `[Char]'
In the pattern: "You"
In a case alternative: "You" -> "u" : hep xs
In the expression: case a of { "You" -> "u" : hep xs }
来自以下代码
hep [] = []
hep [a:xs] = case a of
"You" -> "u":hep xs
谁能告诉我问题出在哪里?
编辑:
我添加了以下代码
hep [] = [[]]
hep (a:xs) = case a of
"you" -> "u":hep xs
"are" -> "r":hep xs
"your" -> "ur":hep xs
"boyfriend" -> "bf":hep xs
"girlfriend" -> "gf":hep xs
"great" -> "gr8":hep xs
a -> a:hep xs
现在我如何能够添加一个大小写,以便如果列表中包含 2 或 3 个特定单词的顺序,我可以将其转换为首字母缩略词?
前
["By","The","way"] = ["btw"]
【问题讨论】:
-
这给了我与“You”完全相同的错误 -> 'u'
-
对我来说很好用。我在返回列表的末尾得到了一个额外的“”,不确定这是不是有意的
-
奇怪,现在它对我有用,哈哈。我将如何再添加 2 个案例,以便我可以比较三个单词并缩写它?示例:“By”、“The”、“way”会变成“btw”