我正在编写一个Haskell程序作为学校的作业。这是一个英语约会程序,最终导致与你完美匹配的“聊天”。我希望聊天能够以瑞典语切换和写入,这意味着该程序需要一个代码用于字母åäö。我怎么做?
import Data.Char
kons = "qwrtpsdfghjklzxcvbnmQWRTPSDFGHJKLZXCVNM"
veral = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
gemen:: Char -> Char
gemen 'A' = 'a'
gemen 'B' = 'b'
gemen 'C' = 'c'
gemen 'D' = 'd'
gemen 'E' = 'e'
gemen 'F' = 'f'
gemen 'G' = 'g'
gemen 'H' = 'h'
gemen 'I' = 'i'
gemen 'J' = 'j'
gemen 'K' = 'k'
gemen 'L' = 'l'
gemen 'M' = 'm'
gemen 'N' = 'n'
gemen 'O' = 'o'
gemen 'P' = 'p'
gemen 'Q' = 'q'
gemen 'R' = 'r'
gemen 'S' = 's'
gemen 'T' = 't'
gemen 'U' = 'u'
gemen 'V' = 'v'
gemen 'W' = 'W'
gemen 'X' = 'x'
gemen 'Y' = 'y'
gemen 'Z' = 'z'
gemen _ = 'a'
dial = do
print "Hello, what's your name?"
name <- getLine
print ("Hi there " ++ name ++ "!")
print "Where do you live?"
place <- getLine
print ("Oh really " ++ name ++ ", so do I!")
print ("Do you wanna switch to swedish?")
svar <- getLine
if svar == "yes" then print ("Kanon, da gor vi det!") else print ("English is just as good!")
perify :: [Char] -> [Char]
perify xs
| (head (xs)) == 'P' = ", " ++ xs
| (head (xs)) `notElem` kons = ", P" ++ [(gemen (head (xs)))] ++ (tail (xs))
| otherwise = ", P" ++ (tail (xs))
答案 0 :(得分:8)
您正在使用print
打印字符串。这不仅会使用转义码打印非ASCII字符(以及不可打印的字符和换行符),还会将字符串括在引号中。
出于非调试目的,您应始终使用putStrLn
,而不是print
来打印字符串。这不仅不会弄乱变音符号,而且还会删除不需要的引号,并允许您打印换行符。