gpt4 book ai didi

haskell - 保留算法不正常

转载 作者:行者123 更新时间:2023-12-05 05:33:07 24 4
gpt4 key购买 nike

运行这段代码的输出是不正确的,我不能悲伤地抱住头,不知道我自己的代码哪里错了。我盯着它看了好几个小时。

代码:

import System.IO(isEOF)

reverseIO :: IO ()
reverseIO = do
line <- getLine
done <- isEOF
if null line
then return ()
else if done then
putStrLn $ reverseStr line
else do
reverseIO

reverseStr :: [w] -> [w]
reverseStr [] = []
reverseStr (x:xs) = reverseStr xs ++ [x]

main = do
reverseIO

期望:<强>2。反转并打印出来。

> hello world
world hello

运行:

GHCi, version 8.10.6: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/runner/University-Labs/.ghci
[1 of 1] Compiling Main ( Main.hs, interpreted )
Ok, one module loaded.
 hello world

编辑:Ctrl+D

GHCi, version 8.10.6: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/runner/University-Labs/.ghci
> hello world
dlrow olleh

最佳答案

您必须将 replaceStr 替换为 unwords 。撤销 。单词:

reverseIO = do 
line <- getLine
done <- isEOF
if null line
then return ()
else if done then
putStrLn . unwords . reverse . words $ line
else reverseIO

关于haskell - 保留算法不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73911130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com