Assignment

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Assignment

sqmau
Hi,

I'm new to Squeak, and working through 'Squeak by Example'
(http://www.squeak.org/Documentation/). I'm confused by the example
on page 49 (Method 4.2):

lineCount
"Answer the number of lines represented by the receiver, where every
cr adds one line."
| cr count |
cr Character cr.
count 1 min: self size.
self do:
[:c | c == cr ifTrue: [count count + 1]].
^ count

What exactly does the following line mean?
cr Character cr.

I (as Perl programmer) would write:
cr := Character cr.

and:
[:c | c = cr ifTrue: [count count + 1]].

Can anyone explain the difference?

Thanx,
Maurice
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Assignment

Blake-5
On Thu, 09 Aug 2007 16:24:03 -0700, Maurice van Peursem  
<[hidden email]> wrote:

> What exactly does the following line mean?
> cr Character cr.

A typo? Unless by some means I don't understand a nil object is supposed  
to understand the message "Character".

> I (as Perl programmer) would write:
> cr := Character cr.

I would think that's right.

> [:c | c = cr ifTrue: [count count + 1]].

Smalltalk has "=" and "==" as assignment and comparison respectively. So  
you need the "==" to compare c to cr.

It also should be "count := count + 1", I believe.
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Assignment

Benjamin Schroeder-2

On Aug 9, 2007, at 7:45 PM, Blake wrote:

>> What exactly does the following line mean?
>> cr Character cr.
>
> A typo? Unless by some means I don't understand a nil object is  
> supposed to understand the message "Character".
>
>> I (as Perl programmer) would write:
>> cr := Character cr.
>
> I would think that's right.
>
>> [:c | c = cr ifTrue: [count count + 1]].
>
> Smalltalk has "=" and "==" as assignment and comparison  
> respectively. So you need the "==" to compare c to cr.
>
> It also should be "count := count + 1", I believe.

I agree with Blake - I think it's a typo and should be ":=", for  
assignment.

To clarify, "=" and "==" are different variants of comparison.  
Usually "=" is used for value comparison - for example, two different  
lists that had the same contents would be "=" equal. "==" means "is  
the same object".

To illustrate, you can try printing

        #(a b c) = #(a b c) copy "true"
        #(a b c) == #(a b c) copy "false"

(For many objects, these concepts are one and the same - they don't  
have any separate idea of value equality, and the default "=", on  
Object, is implemented in terms of "=".)

In my experience, it's idiomatic to use "=" for most things, and use  
"==" when you really mean "must be the same object".

Hope this helps,
Benjamin Schroeder

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Assignment

Mathieu SUEN
In reply to this post by Blake-5
#=
is equality comparaison

#==
is identity comparison

All the character is unique so is better (faster) to use #==.

You can see the default implementation of #= in Object

Object>>= other

        ^self == other

        Mth



On Aug 10, 2007, at 1:45 AM, Blake wrote:

> Smalltalk has "=" and "=="

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Assignment

Blake-5
In reply to this post by Benjamin Schroeder-2
On Thu, 09 Aug 2007 16:57:19 -0700, Benjamin Schroeder  
<[hidden email]> wrote:

> To clarify, "=" and "==" are different variants of comparison. Usually  
> "=" is used for value comparison - for example, two different lists that  
> had the same contents would be "=" equal. "==" means "is the same  
> object".

Right! Sorry, I had slipped into C mode there, where assignment is "=" and  
comparison is "==".
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Assignment

sqmau
In reply to this post by sqmau
>On Thu, 09 Aug 2007 16:24:03 -0700, Maurice van Peursem
><[hidden email]> wrote:
>
>>What exactly does the following line mean?
>>cr Character cr.
>
>A typo? Unless by some means I don't understand a nil object is
>supposed to understand the message "Character".

Ah, that would explain a lot. Very confusing when there are typos in
a teaching manual...

>>I (as Perl programmer) would write:
>>cr := Character cr.
>
>I would think that's right.
>
>>[:c | c = cr ifTrue: [count count + 1]].
>
>Smalltalk has "=" and "==" as assignment and comparison
>respectively. So you need the "==" to compare c to cr.

No, I'm pretty sure you are wrong here, := is assignment, = means
equals, and == means identical. The difference is not explained, but
I take it as value equals vs. pointer equals. That is why I'm
confused, I would expect an = here, not an ==.

http://wiki.squeak.org/squeak/5699

But there are errors in this document also, I hate it when I can't be
sure of a supposedly complete reference.

>It also should be "count := count + 1", I believe.

Yeah, the assignment is lost in all statements. Maybe they used the
underscore, and that got lost in the PDF?

Maurice
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Assignment

Yoshiki Ohshima
In reply to this post by Mathieu SUEN
  Mathieu,

> #=
> is equality comparaison
>
> #==
> is identity comparison

  Yes.

> All the character is unique so is better (faster) to use #==.

  This is not true for Characters whose code is bigger than 256.

  To me, the identity comparison is more or less in the "meta" level
or touching implementation details.  Unless you eally need to check
the "identity" for a good reason, you should always use #= for
comparison, I think.  (Some performance critical potion of code could
be exempted... but it should still be limited very carefully.)

-- Yoshiki
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Assignment

Blake-5
In reply to this post by sqmau
On Thu, 09 Aug 2007 17:25:23 -0700, Maurice van Peursem  
<[hidden email]> wrote:

> Yeah, the assignment is lost in all statements. Maybe they used the  
> underscore, and that got lost in the PDF?

It happens. I think I've seen that in other books as well. Though,  
initially, they probably used the <-
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Identity vs equality (was Re: Assignment)

Göran Krampe
In reply to this post by Yoshiki Ohshima
Hi!

(cc to squeak-dev)

>> All the character is unique so is better (faster) to use #==.
>
>   This is not true for Characters whose code is bigger than 256.
>
>   To me, the identity comparison is more or less in the "meta" level
> or touching implementation details.  Unless you eally need to check
> the "identity" for a good reason, you should always use #= for
> comparison, I think.  (Some performance critical potion of code could
> be exempted... but it should still be limited very carefully.)
>
> -- Yoshiki

Just wanted to mention that this came up on IRC the other day and IMHO one
can generally go by this rule - use #= for equality and only use #== if
you actually *intend* to check for identity. It is slightly poor style to
use #== just to gain a bit of speed, when you in fact *mean* equality.

If we generalise the rule is - program by intention as much as possible
and instead focus on Compiler and friends for speed tricks. :)

It might be interesting to hear what Bryce thinks about this - could for
example Exupery get "tricked" by using #== when you actually mean #= and
actually end up making slower code than if the developer had used #=?

In this particular case it may very well be so that identity checks are
faster than any conceivable equality check - but who knows. :)

regards, Göran

PS. Sure, I have also broken this rule, no doubt about that. ;)

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners