【发布时间】:2019-04-19 07:13:46
【问题描述】:
我正在使用 Ullman(M97) 第二版中的问题练习 sml。我目前正在处理的问题需要一个 piglatin 函数,它接收一个单词,分解它,并检查第一个字符是否是元音(a、e、i、o u)。如果它是元音,它会将字符列表内爆回字符串并在末尾添加“yay”。如果第一个字符不是元音,则该函数将检查其余字符,直到遇到第一个元音。当它这样做时,它将所有出现在第一个元音之前的字符放在字符列表的末尾,将新的字符列表内爆成一个字符串并在其中添加“ay”。
例如:
- pl "able";
val it = "ableyay" : string
- pl "stripe";
val it = "ipestray" : string
fun isVowel (c::cs) =
if c = #"a" then true
else if c = #"e" then true
else if c = #"i" then true
else if c = #"o" then true
else if c = #"u" then true
else false
fun cycle nil = nil
| cycle (h :: hs) = hs @ [h]
fun aL (h::hs) =
if isVowel(h) = true
then h :: hs
else aL (cycle (h :: hs))
fun plx (x) =
if isVowel x = true
then (implode x) ^ "yay"
else implode (aL (x)) ^ "ay"
fun pl (x) = plx (explode x)
我已经解决了大部分问题,但我不知道为什么我的 plx 函数给了我这个:
Error: operator and operand don't agree [tycon mismatch]
operator domain: char list list
operand: char list
in expression: aL x uncaught exception Error
我不知道如何解决它。
【问题讨论】: