add something to a list

Cagdas Ozgenc co19@cornell.edu
Sun, 17 Feb 2002 19:04:58 +0200


>
> You seem to expect currentPath to be updated by putpiece? This won't
happen
> in Haskell. Once you've declared
>        currentPath=[]
> it will always be [].
>
> Values never change. If you want the functional equivalent of accumulator
> variables they have to be an argument of a recursive function. So try
this..
>
> getPath :: Path -> IO Path
> getPath currentPath = do
> piece <- getLine
> if piece == "" then return currentPath
> else getPath (piece:currentPath)
>
> initialCurrentPath::Path
> initialCurrentPath = []
>
> main :: IO ()
> main = do
> path <- getPath initialCurrentPath
> putStrLn (show path)
>
> Regards
> --
> Adrian Hey


Hi Adrian,

How can I add a function that sorts this list that I read from the user and
accumulate using the function that you described? I am not asking for a sort
algorithm of course, I am just wondering how to feed the IO Path as an input
to a sort function? Is it suppose to look like this:

sort :: IO Path -> IO Path

or

sort :: IO Path -> Path

How do you iterate over IO Path?

Thanks for taking time.