Quantcast
Viewing all articles
Browse latest Browse all 3

Answer by ErikR for Haskell data type list

As you probably have been told, Haskell "variables" are not mutable. That means you have to explicitly pass state around as a parameter in your loop.

For example, this is a loop which will repeatedly ask the user for a color:

loop1 = do putStrLn "Enter a color: "           color <- getLine           loop1

However, the users's input is not "saved" anywhere. The next step is to keep track of all of previous input as a parameter to the looping function:

loop2 colors = do putStrLn $ "Previous colors: "++ show colors                  putStrLn "Enter a color: "                  color <- getLine                  loop2 (color:colors)

Hopefully this helps.


Viewing all articles
Browse latest Browse all 3

Trending Articles