The permut2 problem requires something along the lines of indexing an array and for Haskell, indexing long arrays can be a hard task because it has single-linked lists. However, I’m pretty new to Haskell since I’ve only known it for two weeks, so maybe I’m just not thinking in Haskell. Does anyone have a Haskell solution to this problem that passed, or is this just something not for functional programming languages?
Here’s what I tried, but it was too slow:
import System.IO import Control.Monad checkPos :: [Int] -> (Bool, Int) -> Int -> (Bool, Int) checkPos reals (isOK, num) ind = (if isOK then (reals !! (ind-1)) == num+1 else False, if isOK then num+1 else num) main = do num <- getLine let real = read num if (real /= 0) then do nums <- getLine let reals = map read $ words nums if (fst $ foldl (checkPos reals) (True, 0) reals) then putStrLn "ambiguous" else putStrLn "not ambiguous" main else putStr ""