In a file I am saving a file path. I want to read the file path from a file and then convert it to a variable, so everytime I run a function it gets the current file path from the file and then I can use this String in another function. I know variables are immutable but is there a way I can do it?
Here as you can see I am trying to read the file path from the file and then check if it is correct:
checksForCdCommand dir = checkIfPathExists constructedPath $ concat' $ paths sample2 where
constructedPath = do
let file = "abc.txt"
contents <- readFile file
return (if contents /= "/" then contents ++ "/" ++ dir else contents ++ dir)
Here if the contents is "/" it means that its my root dir so thats why I have this if else clause. But this way the code wont compile due to the following:
* Couldn't match expected type `[Char]'
with actual type `IO [Char]'
* In the first argument of `checkIfPathExists', namely
`constructedPath'
In the expression: checkIfPathExists constructedPath
In the expression:
checkIfPathExists constructedPath $ concat' $ paths sample2
I want to do something like this:
checksForCdCommand dir = checkIfPathExists constructedPath $ concat' $ paths sample2 where
constructedPath = if currDir /= "/" then currDir ++ "/" ++ dir else currDir ++ dir
where currDir always has the value from the file.
The idea is that I have a tree that represents a file system and with the func paths I get all of the paths to the children, with checkIfPathExists I pass a String as an argument to see if it's part of all the paths and I am doing a command that is like the cd unix command and when I change directories I save the current dir in the file.
I need this current directory for other things and that's why I need to read it from a file and then change the variable currDir so I can use it for other functions.
cdcommand should have a top-level structure: load the tree, load the current path, from the parameter, take a new path, construct an absolute new path, check if the new path exists, write a new path, end.mkdirshould be all in thedoblock again: load the tree, load the current path, load the created path from the parameter, construct an absolute path, check whether it does not exist, add to the tree, write a new tree, end. This allows reliable functions to remain separate from untrusted IO state.