[Haskell-cafe] Can I destructive rebind a local variable in haskell?

Wang, Chunye (NSN - CN/Beijing) chunye.wang at nsn.com
Tue Jan 6 02:47:53 EST 2009


Hi Luke
 
    Thank you ! I got it :)
 
Best Regards 
Chunye Wang <chunye.wang at nsn.com> 
 

________________________________

From: ext Luke Palmer [mailto:lrpalmer at gmail.com] 
Sent: Tuesday, January 06, 2009 3:44 PM
To: Wang, Chunye (NSN - CN/Beijing)
Cc: Haskell-Cafe at haskell.org
Subject: Re: [Haskell-cafe] Can I destructive rebind a local variable in
haskell?


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/bfbe57b8/attachment.htm


More information about the Haskell-Cafe mailing list