[Haskell-cafe] Can I destructive rebind a local variable in
haskell?
Luke Palmer
lrpalmer at gmail.com
Tue Jan 6 02:44:25 EST 2009
2009/1/6 Wang, Chunye (NSN - CN/Beijing) <chunye.wang at nsn.com>
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090106/f8219629/attachment.htm
More information about the Haskell-Cafe
mailing list