我目前正在Haskell的(https://adventofcode.com/2018)进行adventofcode挑战,以更好地了解该语言。
我的问题是,当我尝试启动我的应用程序时,它只是在执行主要功能后挂起,没有任何错误。当我尝试在ghci内部执行函数时,似乎没有任何问题,当我尝试在calc
函数内部传递一个List时,它也可以在main内部运行并输出结果。
import Data.Maybe
import Control.Monad
main :: IO()
main = do
file <- readFile "./input.txt"
let linesOfFile = lines file
print $ calc (linesOfFile) (0, [])
calc :: [String] -> (Int, [Int]) -> Int
calc arr preState = let
state = foldl (flip nextStep) preState arr
past = snd state
duplicate = repeated past
in if isJust duplicate
then fromJust duplicate
else calc arr state
nextStep :: String -> (Int, [Int]) -> (Int , [Int])
nextStep str state
| head str == '-' = let newState = (fst state) - (read $ tail str :: Int)
pastStates = snd state
in (newState, pastStates ++ [newState])
| head str == '+' = let newState = (fst state) + (read $ tail str :: Int)
pastStates = snd state
in (newState, pastStates ++ [newState])
repeated :: [Int] -> Maybe Int
repeated xs = go xs []
where
go [] _ = Nothing
go (x : xs) visited =
if x `elem` visited
then Just x
else go xs (x : visited)
input.txt位于({https://pastebin.com/7zhWBQr8)
和Github链接:https://github.com/mvorwieger/advent-of-code/tree/master/haskell/2
是没有解决办法吗?