new Smalltalk programmer's thoughts

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

new Smalltalk programmer's thoughts

Wolfgang Helbig-2
Hi list,
at
        http://people.squeakfoundation.org/article/57.html

I wrote down my thoughts while learning how to programm in Smalltalk.

Greatings,
Wolfgang


Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

tblanchard
Welcome!  I've posted a reply BTW.

On Apr 30, 2006, at 10:19 PM, Wolfgang Helbig wrote:

> Hi list,
> at
> http://people.squeakfoundation.org/article/57.html
>
> I wrote down my thoughts while learning how to programm in Smalltalk.
>
> Greatings,
> Wolfgang
>
>


Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

stéphane ducasse-2
And I like your reply :)
even if the point of wolfgang was valid but from a C perspective.

> Welcome!  I've posted a reply BTW.
>
> On Apr 30, 2006, at 10:19 PM, Wolfgang Helbig wrote:
>
>> Hi list,
>> at
>> http://people.squeakfoundation.org/article/57.html
>>
>> I wrote down my thoughts while learning how to programm in Smalltalk.
>>
>> Greatings,
>> Wolfgang
>>
>>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

Wolfgang Helbig-2
In reply to this post by Wolfgang Helbig-2
Hi Stev,
you wrote:
>even if the point of wolfgang was valid but from a C perspective.

and I must frankly admit, that I can't parse that sentence. Ascribe this
to my mother tongue not being English.

So in case you meant my article is valid only from a C programmer's perspective,
let me stress, that my perspective is from a programmer who aims to convince
himself that the program at hand is correct. If you want to attribute this
frame of mind to a profession, I think a mathematician trying to prove a theorem
is more accurate than a C programmer.

Proving a program being correct while writing it is much easier, if the first
natural number is zero. This convention leads to more elegant and "natural"
statements about the state of instance variables.

This is not confined to indexes, but file stream positions as well. The position
is zero after initialization and thus does not address a byte. It needs to be
incremented before it is a valid pointer. This made it much harder to write
and prove the AltoFileStream class correct.

Unix and C let zero be the first natural number. As you might know, the Unix V6
kernel does not contain a single bug. Ironically, the only V6 program
that I consider buggy is the debugger. Maybe this is because Ken Thompson and
Dennis Ritchie didn't need the debugger. And maybe this is because they were
trying to prove their programs to be correct while writing. And maybe this
is the reason they decided to let zero be the first natural number.

The last sentences are purely speculative and Dennis might shed some light on
them. But it is not speculative that Smalltalk is the only programming language
that allows indexed variables and forces the programmer to use one as the first
index. If you find another one let me know.

Greetings,
Wolfgang

--
Weniger, aber besser.


Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

Hans-Martin Mosner
Wolfgang Helbig wrote:

>
>The last sentences are purely speculative and Dennis might shed some light on
>them. But it is not speculative that Smalltalk is the only programming language
>that allows indexed variables and forces the programmer to use one as the first
>index. If you find another one let me know.
>  
>
http://en.wikipedia.org/wiki/Array lists BASIC and Fortran as having
1-based array indexing, and it even discusses some of the relative
merits of and arguments for 0-based and 1-based array indexing.
Which one is more natural is a matter of personal taste and opinion. In
most everyday programming situations I consider the 1-based approach
much more natural, but the low-level stuff where you're addressing
hardware is definitely an exception which can be handled pretty well by
implementing a specialized arrayed class which does 0-based indexing.
Having learned BASIC as a first language in school, I found C's
convention of 0-based indexing pretty awkward, until I realized that C
is mostly a kind of portable assembly language which lets you get close
to the hardware. It's not a language I would consider well suited for
expression my thoughts in a program...
The question is: do we want to let hardware characteristics dominate our
way of thinking? I at least don't. When I've got 5 elements in a
collection, I want the first at index 1 and the fifth at index 5. When
I'm using Integers, I don't want to think about 16 vs 32 bits, signed vs
unsigned etc.
The argument about loop bounds is somewhat related. I've seen quite some
beginner's C code which did "for (i=0; i<=10; i++)" to address a
10-element array :-) Seasoned C programmers don't do that anymore, but
it tells me a bit about what is "natural" :-)

Cheers,
Hans-Martin

Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

Wolfgang Helbig-2
In reply to this post by Wolfgang Helbig-2
Hi Hans-Martin,
you mentioned:

>http://en.wikipedia.org/wiki/Array lists BASIC and Fortran as having
>1-based array indexing

not any more :-)

In Fortran 77 you specify a Real Array with indexes from 0 to 10 as
        REAL(0:10)
This leaves BASIC and Smalltalk on the wikipedia list.

Greetings,
Wolfgang
--
Weniger, aber besser.


Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

Wolfgang Helbig-2
In reply to this post by Wolfgang Helbig-2
Hi Hans-Martin,
you posed the question
>The question is: do we want to let hardware characteristics dominate our
>way of thinking?

And the answer is no!
And I deeply appreciate Smalltalk for the #// and #\\ operators, for Integers
that are unlimeted, and for things like garbage collection that no real machine
can achieve! Smalltalk leverages on being run by a virtual machine. And it does
very well so. Furthermore, it lets you adopt things to your likes much easier
than any other language I know. (I don't know Lisp) So, in this respect, I
couldn't agree more with you.

>collection, I want the first at index 1 and the fifth at index 5. When
>I'm using Integers, I don't want to think about 16 vs 32 bits, signed vs
>unsigned etc.
>The argument about loop bounds is somewhat related. I've seen quite some
>beginner's C code which did "for (i=0; i<=10; i++)" to address a
>10-element array :-) Seasoned C programmers don't do that anymore, but
>it tells me a bit about what is "natural" :-)

And that is my point: Different challenges need different numbers for the first
index. When I am implementing a Gaussian algorithm to invert matrices, I'd never
use 0 as the first index. But whenever I am computing indexes, I'd never use
one as the first index. And this is not supported by BASIC and Smalltalk.

Greetings,
Wolfgang
>
>Cheers,
>Hans-Martin

--
Weniger, aber besser.


Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

Philippe Marschall
In reply to this post by Wolfgang Helbig-2
2006/5/1, Wolfgang Helbig <[hidden email]>:

> Hi Hans-Martin,
> you mentioned:
>
> >http://en.wikipedia.org/wiki/Array lists BASIC and Fortran as having
> >1-based array indexing
>
> not any more :-)
>
> In Fortran 77 you specify a Real Array with indexes from 0 to 10 as
>         REAL(0:10)
> This leaves BASIC and Smalltalk on the wikipedia list.

IIRC XPath uses 1 based indices too.

Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

Alejandro F. Reimondo
In reply to this post by Wolfgang Helbig-2
Hi Wolfgang,
Smalltalk, as an ambience, can be used
 for doing other activities than writing "programs"
 (aProgram, aComponent, or aModule).
When you compare Smalltalk with languages you are
 comparing syntax (the only thing comparable from
 a language perspective).
Newbies use to compare smalltalk from language
 perspective, or from OO way (through design,
 abstractions, patters) to the tools they used
 in the past.
Only with time, opportiunities of real use in production
 and contact with people that use smalltalk in another
 way (not simply like the best OOL); new ways of
 doing informatics emerges.
Smalltalk as a open system, let you do non-formal
 development (e.g. pass the limits imposed by
 formalization, reductionism & encapsulation).
As a non-formal device/medium, it is not the
 opposite of formal design; it is a convenient complement
 (the only one competitive complement to formal
 design used in software industry today)
best,
Ale.


----- Original Message -----
From: "Wolfgang Helbig" <[hidden email]>
To: <[hidden email]>
Sent: Monday, May 01, 2006 9:02 AM
Subject: Re: new Smalltalk programmer's thoughts


> Hi Hans-Martin,
> you posed the question
> >The question is: do we want to let hardware characteristics dominate our
> >way of thinking?
>
> And the answer is no!
> And I deeply appreciate Smalltalk for the #// and #\\ operators, for
Integers
> that are unlimeted, and for things like garbage collection that no real
machine
> can achieve! Smalltalk leverages on being run by a virtual machine. And it
does
> very well so. Furthermore, it lets you adopt things to your likes much
easier

> than any other language I know. (I don't know Lisp) So, in this respect, I
> couldn't agree more with you.
>
> >collection, I want the first at index 1 and the fifth at index 5. When
> >I'm using Integers, I don't want to think about 16 vs 32 bits, signed vs
> >unsigned etc.
> >The argument about loop bounds is somewhat related. I've seen quite some
> >beginner's C code which did "for (i=0; i<=10; i++)" to address a
> >10-element array :-) Seasoned C programmers don't do that anymore, but
> >it tells me a bit about what is "natural" :-)
>
> And that is my point: Different challenges need different numbers for the
first
> index. When I am implementing a Gaussian algorithm to invert matrices, I'd
never
> use 0 as the first index. But whenever I am computing indexes, I'd never
use

> one as the first index. And this is not supported by BASIC and Smalltalk.
>
> Greetings,
> Wolfgang
> >
> >Cheers,
> >Hans-Martin
>
> --
> Weniger, aber besser.
>
>


Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

Nicolas Cellier-3
In reply to this post by Wolfgang Helbig-2

Hi Wolfgang,

Your point of view is correct, but sorry, it is a C point of view. That means
a point of view of how can i better insure the low level implementation.

And in fact, i trust you, it is more convenient to have 0-based if your are
dealing with pointers and offset.

But the aim of Smalltalk was not to ease such implementation. Smalltalk ignore
the notion of pointer, and assume all this low level goes into the VM.

Smalltalk goal is to ease the upper level programming. And at upper level,
sorry, if i want the object of rank 100 in a collection, i naturally write
(collec at: 100), and would find it very irritating to write (collec at: 99),
having to think of how the processor will translate this into an offset.

Looking at the history of numbers, you will discover that the zero is a
relatively new invention. Initially, it just meant empty, none or nihil (our
Smalltalk nil).

And looking closer at the english words, you know very well that fourth must
be associated to number four, fifth to number five, etc...
If you associate first to 0, then you associate fourth to 3, something that
does not sound natural to me.

That is why we index starting at 1 in Smalltalk, and why we are reluctant to
change that (though it is not at all in the syntax, just a convention choice
in the library).

Beside, it does not prevent us to use 0-based algorithm like with Stream
position, when what we need is an offset. So i do not really see that as an
obstacle.
It is just a programming habit, and i can tell you i also made a lot of
index-errors when switching between FORTRAN and C code, so the lack of errors
is not an objective criterium...

Nicolas


Reply | Threaded
Open this post in threaded view
|

0 or 1 index (was: new Smalltalk programmer's thoughts)

Jecel Assumpcao Jr
In reply to this post by Wolfgang Helbig-2
Wolfgang Helbig wrote on Mon, 1 May 2006 14:02:19 +0200 (MEST)
> And that is my point: Different challenges need different numbers for the first
> index. When I am implementing a Gaussian algorithm to invert matrices, I'd never
> use 0 as the first index. But whenever I am computing indexes, I'd never use
> one as the first index. And this is not supported by BASIC and Smalltalk.

To be fair, I have used more than one BASIC that allowed you to set
either 0 or 1 as the base index for all arrays in a program. And the
Self dialect of Smalltalk (http://www.merlintec.com:8080/Self/) does
borrow several C conventions including 0 based arrays.

The current program I am writing in Squeak simulates hardware and 1
based arrays were a bit inconvenient, so the first thing I did was
define a ZArray class as a subclass of ArrayedCollection with

at: index
  ^ super at: index + 1

at: index put: obj
  ^ super at: index + 1 put: obj

do: block
   0 to: self size - 1 do:
         [ :index | block value: (self at: index) ]

and then all I had to do was write the rest of my application naturally
and just remember to use "ZArray new: x" instead of "Array new: x".
Inspecting ZArrays still shows the contents as having indexes starting
with 1, but other than that small confusion (which I could easily fix if
I wanted to) it works just great. There is probably a significant
performance hit but I am not worried about that here.

-- Jecel

Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

Laurence Rozier
In reply to this post by Hans-Martin Mosner


On 5/1/06, Hans-Martin Mosner <[hidden email]> wrote:
Wolfgang Helbig wrote:

>
>The last sentences are purely speculative and Dennis might shed some light on
>them. But it is not speculative that Smalltalk is the only programming language
>that allows indexed variables and forces the programmer to use one as the first
>index. If you find another one let me know.
>
>
http://en.wikipedia.org/wiki/Array lists BASIC and Fortran as having
1-based array indexing, and it even discusses some of the relative
merits of and arguments for 0-based and 1-based array indexing.
Which one is more natural is a matter of personal taste and opinion. In
most everyday programming situations I consider the 1-based approach
much more natural, but the low-level stuff where you're addressing
hardware is definitely an exception which can be handled pretty well by
implementing a specialized arrayed class which does 0-based indexing.
Having learned BASIC as a first language in school, I found C's
convention of 0-based indexing pretty awkward, until I realized that C
is mostly a kind of portable assembly language which lets you get close
to the hardware. It's not a language I would consider well suited for
expression my thoughts in a program...
The question is: do we want to let hardware characteristics dominate our
way of thinking? I at least don't. When I've got 5 elements in a
collection, I want the first at index 1 and the fifth at index 5. When
I'm using Integers, I don't want to think about 16 vs 32 bits, signed vs
unsigned etc.
The argument about loop bounds is somewhat related. I've seen quite some
beginner's C code which did "for (i=0; i<=10; i++)" to address a
10-element array :-) Seasoned C programmers don't do that anymore, but
it tells me a bit about what is "natural" :-)

So true. The admonition "remember arrays start at zero" is repeated countless times in most literature. You see this in just about every article or book on most modern languages. If it were so "natural" to count groups of things from 0 people wouldn't need to be reminded constantly.

Regards,
Laurence

Cheers,
Hans-Martin




Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

Laurence Rozier
In reply to this post by Philippe Marschall


On 5/1/06, Philippe Marschall <[hidden email]> wrote:
2006/5/1, Wolfgang Helbig <[hidden email]>:

> Hi Hans-Martin,
> you mentioned:
>
> > http://en.wikipedia.org/wiki/Array lists BASIC and Fortran as having
> >1-based array indexing
>
> not any more :-)
>
> In Fortran 77 you specify a Real Array with indexes from 0 to 10 as
>         REAL(0:10)
> This leaves BASIC and Smalltalk on the wikipedia list.

IIRC XPath uses 1 based indices too.

Applescript and COBOL do as well.




Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

Petite Abeille-2

On May 01, 2006, at 19:25, Laurence Rozier wrote:

> On 5/1/06, Philippe Marschall <[hidden email]> wrote:
>>
>> 2006/5/1, Wolfgang Helbig <[hidden email]>:
>> > This leaves BASIC and Smalltalk on the wikipedia list.
>>
>> IIRC XPath uses 1 based indices too.
>
>
> Applescript and COBOL do as well.

Lua to!

http://lua-users.org/wiki/CountingFromOne

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/


Reply | Threaded
Open this post in threaded view
|

Re: 0 or 1 index (was: new Smalltalk programmer's thoughts)

Laurence Rozier
In reply to this post by Wolfgang Helbig-2


On 5/1/06, Jecel Assumpcao Jr <[hidden email]> wrote:
Wolfgang Helbig wrote on Mon, 1 May 2006 14:02:19 +0200 (MEST)
> And that is my point: Different challenges need different numbers for the first
> index. When I am implementing a Gaussian algorithm to invert matrices, I'd never
> use 0 as the first index. But whenever I am computing indexes, I'd never use
> one as the first index. And this is not supported by BASIC and Smalltalk.

To be fair, I have used more than one BASIC that allowed you to set
either 0 or 1 as the base index for all arrays in a program. And the
Self dialect of Smalltalk (http://www.merlintec.com:8080/Self/) does
borrow several C conventions including 0 based arrays.

The current program I am writing in Squeak simulates hardware and 1
based arrays were a bit inconvenient, so the first thing I did was
define a ZArray class as a subclass of ArrayedCollection with

at: index
  ^ super at: index + 1

at: index put: obj
  ^ super at: index + 1 put: obj

do: block
   0 to: self size - 1 do:
         [ :index | block value: (self at: index) ]

and then all I had to do was write the rest of my application naturally
and just remember to use "ZArray new: x" instead of "Array new: x".
Inspecting ZArrays still shows the contents as having indexes starting
with 1, but other than that small confusion (which I could easily fix if
I wanted to) it works just great. There is probably a significant
performance hit but I am not worried about that here.

This seems to me to be a good approach. I think Alan Kay said something like "hardware is just software crystallized early". If one accepts that notion then the special needs of hw should properly be subsets of the typical case. Perhaps, when the world is running on Plurion processors, that will come to pass ;-)

Laurence

-- Jecel




Reply | Threaded
Open this post in threaded view
|

Re[2]: new Smalltalk programmer's thoughts

Herbert König
In reply to this post by Wolfgang Helbig-2
Hello Wolfgang,

WH> index. If you find another one let me know.

Matlab! And there's a big user group using it for dsp (digital signal
processing) which are deeply frustrated about it.

So if you want to look into this discussion search the comp.dsp
archives.

For this discussion:

As a LISPer I'm annoyed by (nth 1 ofMyList) being the second element,
as a DSPer I'm annoyed whenever I read some 1-based matlab indexing
while zero-based indexing feels so natural in DSP.

As a child of two worlds I bloody my nose with off by one errors more
frequent than others but by now I have a rugged nose (TM), so I just
laugh and change the indexing.

Cheers

Herbert                            mailto:[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

Yoshiki Ohshima
In reply to this post by Wolfgang Helbig-2
  Wolfgang,

> Unix and C let zero be the first natural number. As you might know, the Unix V6
> kernel does not contain a single bug. Ironically, the only V6 program
> that I consider buggy is the debugger. Maybe this is because Ken Thompson and
> Dennis Ritchie didn't need the debugger. And maybe this is because they were
> trying to prove their programs to be correct while writing. And maybe this
> is the reason they decided to let zero be the first natural number.

  The people's favorite language Perl has a special variable "$[" that
let you specify the index of the first element in an array.

  As everyone wrote: it is about either to think about the n-th index
, or the offset or distance and I'd say that you can't simply say
which is more natural.

  Also, in Smalltalk, Streams are 0-based as nicolas and others
mentioned.

a _ ReadStream on: 'ABC'.
a next. " => $A"
a position. "=> 1"
a position: 0.
a next. "=> $A"
a position: 3.
a next. "=> nil"

  The AltoFileStream should've follow this convension, instead of
allowing [1..size] indices.

  (The coordinates in a Form are 0-based, by the way...)

  Just my $0.02,

-- Yoshiki

Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

Wolfgang Helbig-2
In reply to this post by Wolfgang Helbig-2
Hi Yoshiki,
as you and others pointed out:
>  Also, in Smalltalk, Streams are 0-based as nicolas and others
>mentioned.
>  The AltoFileStream should've follow this convension, instead of
>allowing [1..size] indices.

I agree! It not only *should* follow this convention, but it *must* follow it.

After all, I programmed AltoFileStream to be used by the Hobbes Emulator, which
expects a FileStream to read the snapshot. So I subclassed FileStream and tried
to implement its protocol. Later I found out, that ReadStream suffices for this
purpose. Anyway, I had to implement the semantics of #postion and #position: as
expected by the Hobbes Emulator. Since I didn't find a specification of that
protocol, I naively assumed that position is supposed to be in [1..size]. After
all, this is the natural range of indexes in Smalltalk, isn't it?

But I was utterly wrong, which I found out when I tried to make Hobbes read the
snapshot from an AltoDisk.

The position is supposed to be in range [0..size]. Which looks very unnatural to
me. With this range, position looses the nice property of being a valid index.
So you have to make sure you never access the byte indexed by the current
position. Instead you always use current position + 1. Unix files are more
elegant in this respect. Their position is in the range [0..size), which happens
to be the range of valid "Unix indexes". And I feel much more comfortable with
[0..size) than with [0..size] or [1..size].

Thanks to everyone on this list for their answers! I've learned a lot!

Greetings
Wolfgang, a humble programmer
--
Weniger, aber besser.


Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

Bill Spight
In reply to this post by Wolfgang Helbig-2
Dear Wolfgang,


> But it is not speculative that Smalltalk is the only programming
> language that allows indexed variables and forces the programmer to
> use one as the first index. If you find another one let me know.
>
To add to the list, there's Icon.

Icon indexes in an interesting way. It indexes not the elements, but
before, after, and between elements. It also indexes from both ends of a
list or array. E. g.,

    (1,-3) A (2,-2) B (3,-1) C (4,0)

There are three elements, A, B, and C, and 8 indices, -3 to 4. 1 and -3
refer to the point before A, 2 and -2 to the point before B, and 3 and
-1 to the point before C. 4 and 0 refer to the end. To access an element
you use one of the indices that comes before it.

Best regards,

Bill


Reply | Threaded
Open this post in threaded view
|

Re: new Smalltalk programmer's thoughts

timrowledge
>
>
>> But it is not speculative that Smalltalk is the only programming
>> language that allows indexed variables and forces the programmer to
>> use one as the first index. If you find another one let me know.

Even if it were true that no other language used 1 origin array  
indexing, so what? Popularity is no indication of correctness. If we  
cared about being like all the other languages we wouldn't have  
Smalltalk. If we cared about being like all the other kids we  
wouldn't have geeks like us to create stuff like Smalltalk. And then  
where would the world be?

Ultimately, Smalltalk, like all other languages, systems, cars,  
planes, boats, foods, political parties, comics, games etc is what it  
is. If you like it - Enjoy! If you don't - find one you do like and  
Enjoy!


tim
--
tim Rowledge; [hidden email]; http://www.rowledge.org/tim
Asking whether machines can think is like asking whether submarines  
can swim.



12