0

嘿伙计们,所以我想获取一个单词列表并返回一个与其类似的列表,但每次出现连续单词时都会进行以下替换。

一个例子是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"]
4

2 回答 2

3

您正在尝试匹配字符串列表的列表,但类型为hepis [Word] -> [Word],这与此相矛盾。错误消息指的是这个事实。

但我猜你真正想要的是这个?

hep [] = []
hep (a:xs) = case a of 
    "You" -> "u":hep xs
    a -> a:hep xs
于 2013-02-13T03:29:35.910 回答
0

像这样的东西?

"By" -> let (y:ys) = xs
        in if y=="The" && head ys=="Way"
              then "btw": hep (drop 2 xs)
              else a:hep xs

虽然我不想连续写 50 次。这个怎么样?

import Data.List
import Data.Maybe

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
              "by" -> matchPhrase 3
              "see" -> matchPhrase 2
              a -> a:hep xs
            where matchPhrase num = 
                    let found = lookup (concat $ take num (a:xs)) phrase_list
                    in case found of
                        Just _ -> fromJust found : hep (drop num (a:xs)) 
                        Nothing -> a:hep xs

phrase_list = [("bytheway", "btw"), ("seeyou","cu")]
于 2013-02-13T04:24:55.813 回答