[Haskell-beginners] Re: Non-recursive binding expression

j.romildo at gmail.com j.romildo at gmail.com
Sat Feb 28 17:02:25 EST 2009


On Sat, Feb 28, 2009 at 01:01:56PM -0500, Brent Yorgey wrote:
> On Sat, Feb 28, 2009 at 05:19:03PM +0000, Will Ness wrote:
> >  <j.romildo <at> gmail.com> writes:
> > 
> > > 
> > > Hello.
> > > 
> > > Is there any non-recursive binding expression in Haskell?
> > > 
> > > Something that would allow me to write, for instance,
> > > 
> > >   test = let' xs = [] in
> > >          let' xs = 3 : xs in
> > >          let' xs = 8 : xs in
> > >          let' xs = 7 : xs in
> > >          xs
> > 
> > Well, it's not a nice thing to do (probably), but you can write
> > 
> > test = head $ 
> >   do xs <- [ [] ]
> >      xs <- [ 3:xs ]
> >      xs <- [ 8:xs ]
> >      xs <- [ 7:xs ]
> >      return xs
> > 
> 
> The correct answer is no.  Life is pain.  Anyone who says otherwise is
> selling something. ;)
> 
> Assignment in Haskell is not destructive update---it assigns a name to
> a value, and they become one flesh, until parted by death (i.e. the
> end of their lexical scope).  The only reason Will's code works is
> that each line creates a new lexical scope, and each xs shadows the
> previous one.

I am not asking for assignment. What I want is indeed what I already
said: a new lexical scope which introduces a new variable, but the
variable name is being reused. That shadows the previous definition.

What I want is the possibilitiy of a non-recursive binding expression,
like my hypothecial let' construction. Being non-recursive,

   let' <var> = <exp1> in <exp2>

any occurrence of <var> in <exp1> does not refer to the new varibale
being introduced, but to some previous defined variable named <var>. If
that does exist, than there is a semantic error in the expression
(unless <var> is not used in <exp1>, of course). So the scope of <var>
is just <exp2>. It does not include <exp1>. If it was a recursive
definition, the scope of <var> would be <exp1> *and* <exp2>.

Romildo


More information about the Beginners mailing list