> Hi,
> I know this is really basic, but could someone please give me a simple
> explanation of what LET .. and IN .. means, either generally, or specific
> to Haskell
Let x = expr1 in expr2
means the same as expr2 with all the (free) xs replaced by expr1
Let x = 1 in x + x
means the same as (has the same value as) 1 + 1
so 2+let x = 1 in x+x evaluates to 4
* * *
Alternatively, let <var> = <expr1> in <expr2>
means the same as (\ <var> -> <expr2>) (<expr1>)
--
Jón Fairbairn Jon.Fa...@cl.cam.ac.uk
> Alternatively, let <var> = <expr1> in <expr2>
> means the same as (\ <var> -> <expr2>) (<expr1>)
Yes let is a sugar of special case of application
without considering types. But if the language
uses let-polymorphic type system to check and infer
types then let has different static semantics.
let is used as a syntax giving polymorphic type to the variables.
I'm curious: Is
let p1=e1
p2=e2
....
in e
the same as
case e1 of
p1->case e2 of
p2->... e
?
--
/Times-Bold 40 selectfont/n{moveto}def/m{gsave true charpath clip 72
400 n 300 -4 1{dup 160 300 3 -1 roll 0 360 arc 300 div 1 1 sethsbcolor
fill}for grestore 0 -60 rmoveto}def 72 500 n(This message has been)m
(brought to you by the)m(letter alpha and the number pi.)m(David Feuer)
m(David...@brown.edu)m showpage
Same story here: operationally, both are usually equivalent, but from
the typing point of view there is a difference (let-bound identifiers
may be assigned polymorphic types).
In Haskell, there is also a difference with respect to laziness of
pattern matching. Frankly, I always fail to recall this semantic detail
correctly. I believe let matches lazily, while case and \ are strict.
- Andreas
--
Andreas Rossberg, ross...@ps.uni-sb.de
"Computer games don't affect kids; I mean if Pac Man affected us
as kids, we would all be running around in darkened rooms, munching
magic pills, and listening to repetitive electronic music."
- Kristian Wilson, Nintendo Inc.
> I'm curious: Is
>
> let p1=e1
> p2=e2
> ....
> in e
>
> the same as
> case e1 of
> p1->case e2 of
> p2->... e
>
> ?
No. let matches e1 with p1 lazily: it doesn't check if it matches right
at the beginning, but only when one of variables introduced by p1 is
used. OTOH case begins with evaluating as much of e1 as p1 requires
and checking whether it matches. The same applies to e2 and p2.
When p1 is something like (x,y), which matches every value of the
corresponding type, it's still evaluated at different points of time
by let and case. This matters if e1 happens not to terminate.
Besides this, there is a difference in typing: if e1 is polymorphic
and p1 is a variable with an explicit type signature, different uses
of this variable may choose to instantiate the type differently.
Ignoring that, and ignoring the possibility of recursion (when e1
mentions variables introduced by p1 and p2), 'let p1 = e1 in x'
is equivalent to 'case e1 of ~p1 -> x' (note '~' which makes the
pattern lazy).
--
__("< Marcin Kowalczyk * qrc...@knm.org.pl http://qrczak.ids.net.pl/
\__/
^^
QRCZAK
> Thu, 20 Dec 2001 23:55:51 -0500, David Feuer <David...@brown.edu> pisze:
>
> > I'm curious: Is
> >
> > let p1=e1
> > p2=e2
> > ....
> > in e
> >
> > the same as
> > case e1 of
> > p1->case e2 of
> > p2->... e
> >
> > ?
>
> No. let matches e1 with p1 lazily: it doesn't check if it matches right
> at the beginning, but only when one of variables introduced by p1 is
> used. OTOH case begins with evaluating as much of e1 as p1 requires
> and checking whether it matches. The same applies to e2 and p2.
>
> When p1 is something like (x,y), which matches every value of the
> corresponding type, it's still evaluated at different points of time
> by let and case. This matters if e1 happens not to terminate.
>
> Besides this, there is a difference in typing: if e1 is polymorphic
> and p1 is a variable with an explicit type signature, different uses
> of this variable may choose to instantiate the type differently.
case (e1,e2,...) of
~(p1,p2,...)->e
This gets rid of the recursion problem, but I guess it doesn't fix
polymorphism? I wonder... would it be possible to extend case expressions to
deal with this too? This would make them even more expressive.... i.e., allow
not only pattern bindings but also function bindings. I don't know a good
syntax for this, but maybe the following?
case (#x+1#,q) of
(_,3) -> 4
(f x,_) -> q
Would this kind of thing be possible?