Dear haskeller,
Can I destructive rebind a
local variable like this
import System.Directory
test filename = do
is_dir <- doesDirectoryExist filename
let filename =
if not is_dir then filename else filename
Nope. The "filename" on the right side of the = is the same as the
"filename" on the left, so you're making an infinite loop, the same way:
let x = x in x
is an infinite loop. However you can make a new name as you are
trying, you just can't reference the old one. e.g.:
let filename = 42
Here would be just fine.
However, for cases like this, it is useful that single quote is a valid
identifier, so you can say:
let filename' = if not is_dir then filename else filename
(read "filename prime")
Luke