我创建了一段代码:
intToDigit :: Char -> Int
ord :: Char -> Int
intToDigit c = ord c - ord 'a'
Run Code Online (Sandbox Code Playgroud)
但是,当我运行它时,我收到此错误消息:
ChangeVowels.hs:2:1:`ord'的类型签名缺少附带的绑定
ChangeVowels.hs:4:16:不在范围内:`ord'
ChangeVowels.hs:4:24:不在范围内:`ord'
我尝试过,Import data.char但这也不起作用.
好的,所以我知道这有点不对,但我错过了什么?代码是用以下单词旋转单个字母:
rotate 1 "hello" "ohell"
rotate -1 "hello" "elloh"
Run Code Online (Sandbox Code Playgroud)
我的代码是:
module Main where
import System
main = do
(arg1:_) <- getArgs
xs <- getContents
putStr (rotate xs arg1)
rotate input w1 = unlines [ process line | line <- lines input ]
where process line = unwords [ word | word <- words line ]
map rotate n xs = drop n xs ++ take n xs
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是否正确,任何线索/提示下一步去哪里?
所以有点像这样:
shift :: a -> Int -> a
rotate :: a -> …Run Code Online (Sandbox Code Playgroud) 此文本需要使用自然数向左旋转,并在输入负数时向右旋转.所以:
rotate 1 "foo bar baz" = "ofo rba zba"
rotate (-1) "foo bar baz" = "oof arb azb"
Run Code Online (Sandbox Code Playgroud)
另外我可以将文本分解成行然后单词,我知道所有的行,unlines,words,unwords.我在向左或向右移动文本的定义方面遇到问题,我是否需要使用head函数?