Mark Smithfield | 3 Oct 05:22 2007
Picon

assorted beginner questions

This is a mixed bag. These are the questions that I
would ask an
informed friend. They might be obvious, but they are
not obvious
to me (yet, I hope).
-----------------------------
As I read code in Squeak, I discover method names that
I do not
know. How do you find these methods if you don't know
the type of
the receiver? For example,

    self subclassResponsibility.
-----------------------------
In the floor method from Number,

    truncation _ self truncated.

What does the underscore mean?
-----------------------------
I want to apply smalltalk to fibonacci numbers. I add
this method
to Integer,

fib:
    (self = 0) ifTrue: (^0)
    (self = 1) ifTrue: (^1)
    ^ (self - 1 fib) + (self - 2 fib).

Next, I would like to memoize this method, (because of
(Continue reading)

Blake | 3 Oct 07:21 2007

Re: assorted beginner questions

On Tue, 02 Oct 2007 20:22:01 -0700, Mark Smithfield  
<m_smithfield <at> yahoo.com> wrote:

> In the floor method from Number,
>
>     truncation _ self truncated.
>
> What does the underscore mean?

:=

In some images it shows up as a left-pointing arrow.

I'm not sure if that's coming back in later iterations or not.

> I want to apply smalltalk to fibonacci numbers. I add
> this method
> to Integer,
>
> fib:
>     (self = 0) ifTrue: (^0)
>     (self = 1) ifTrue: (^1)
>     ^ (self - 1 fib) + (self - 2 fib).
>
> Next, I would like to memoize this method, (because of
> the enormous performance gains).  I do not see how to
> memo-ize things in smalltalk. Can somebody help me see the necessary
> shift in thinking?

I don't see why memoization would be different in Smalltalk. Or why it  
(Continue reading)

Mark Smithfield | 3 Oct 15:55 2007
Picon

Re: Beginners Digest, Vol 18, Issue 2

> > I want to apply smalltalk to fibonacci numbers. I
> add
> > this method
> > to Integer,
> >
> > fib:
> >     (self = 0) ifTrue: (^0)
> >     (self = 1) ifTrue: (^1)
> >     ^ (self - 1 fib) + (self - 2 fib).
> >
> > Next, I would like to memoize this method,
> (because of
> > the enormous performance gains).  I do not see how
> to
> > memo-ize things in smalltalk. Can somebody help me
> see the necessary
> > shift in thinking?
> 
> I don't see why memoization would be different in
> Smalltalk. Or why it  
> would specifically have to be, rather. You might
> create a Fibonacci class  
> that contained an array or somesuch and cached the
> numbers that had  
> already been called. (Slowly taking up more and more
> space over time.)

So. You would have an array called Fibs or Primes, and
just stack up a collection of Primes that would hang
around?
(Continue reading)

Ron Teitelbaum | 3 Oct 16:07 2007

RE: assorted beginner questions

Hi Mark,

Welcome to the list, and Smalltalk.

> From: Mark Smithfield
> 
> This is a mixed bag. These are the questions that I
> would ask an
> informed friend. They might be obvious, but they are
> not obvious
> to me (yet, I hope).
> -----------------------------
> As I read code in Squeak, I discover method names that
> I do not
> know. How do you find these methods if you don't know
> the type of
> the receiver? For example,
> 
>     self subclassResponsibility.

This is an error that a programmer uses to call attention to something that
another developer should have done.  In Smalltalk we have something called
inheritance.  This basically means that if you subclass an object the
subclass can be considered to have, or implement, all the methods of its
parent object.  There are times when you want the subclass to provide its
own content in a method but you want to make sure that all subclasses
provide a valid answer to the message.  In that case you implement the
method and have it call self subclassResponsibility.  Which would raise and
error that says, your class should have overridden this method, or something
like that.  This tells you that you made a mistake and should implement a
(Continue reading)

Michael Davies | 3 Oct 16:31 2007
Picon

Re: Re: Beginners Digest, Vol 18, Issue 2

> > > I want to apply smalltalk to fibonacci numbers. I

> > > Next, I would like to memoize this method,

Hi Mark,
By coincidence, I was playing with this myself yesterday. If you're
interested in the numbers rather than the technique, there's a couple
of alternative approaches to speed it up, iteration and matrix
manipulation.

Michael

fibonacci
	"Answer the fibonacci number at the receiver."
	| fib |
	self < 0 ifTrue: [self error: 'Not valid for negative integers'].
	self = 0 ifTrue: [^ 0].	
	self = 1 ifTrue: [^ 1].
	fib := IntegerArray new: self.
	fib at: 1 put: 1.
	fib at: 2 put: 1.
	3 to: self do: [ :each | fib at: each put: ((fib at: (each - 1)) +
fib at: (each - 2))].
	^ fib at: self.

fibonacci
	"Answer the fibonacci number at the receiver."
	| m mx f |
	"Writing f1 = f1 and f2 = f0 + f1 in matrix notation and generalising
	shows us that:
(Continue reading)

Bert Freudenberg | 3 Oct 16:49 2007
Picon

Re: Re: Beginners Digest, Vol 18, Issue 2

On Oct 3, 2007, at 15:55 , Mark Smithfield wrote:

>>> I want to apply smalltalk to fibonacci numbers. I
>> add
>>> this method
>>> to Integer,
>>>
>>> fib:
>>>     (self = 0) ifTrue: (^0)
>>>     (self = 1) ifTrue: (^1)
>>>     ^ (self - 1 fib) + (self - 2 fib).

This is not Smalltalk. Blocks need square brackets, and unary  
messages bind more closely that binary messages (a.k.a. operators):

fib
	^self <= 1
		ifTrue: [self]
		ifFalse: [(self - 1) fib + (self - 2) fib]

>>> Next, I would like to memoize this method,
>> (because of
>>> the enormous performance gains).  I do not see how
>> to
>>> memo-ize things in smalltalk. Can somebody help me
>> see the necessary
>>> shift in thinking?
>>
>> I don't see why memoization would be different in
>> Smalltalk. Or why it
(Continue reading)

Bert Freudenberg | 3 Oct 16:57 2007
Picon

Re: Re: Beginners Digest, Vol 18, Issue 2


On Oct 3, 2007, at 16:31 , Michael Davies wrote:

>>>> I want to apply smalltalk to fibonacci numbers. I
>
>>>> Next, I would like to memoize this method,
>
> Hi Mark,
> By coincidence, I was playing with this myself yesterday. If you're
> interested in the numbers rather than the technique, there's a couple
> of alternative approaches to speed it up, iteration and matrix
> manipulation.
>
> Michael
>
> fibonacci
> 	"Answer the fibonacci number at the receiver."
> 	| fib |
> 	self < 0 ifTrue: [self error: 'Not valid for negative integers'].
> 	self = 0 ifTrue: [^ 0].	
> 	self = 1 ifTrue: [^ 1].
> 	fib := IntegerArray new: self.
> 	fib at: 1 put: 1.
> 	fib at: 2 put: 1.
> 	3 to: self do: [ :each | fib at: each put: ((fib at: (each - 1)) +
> fib at: (each - 2))].
> 	^ fib at: self.
>

Now that is plain silly. If you just iterate, why store in an array?  
(Continue reading)

Bert Freudenberg | 3 Oct 17:03 2007
Picon

Re: Re: Beginners Digest, Vol 18, Issue 2


On Oct 3, 2007, at 16:49 , Bert Freudenberg wrote:

> On Oct 3, 2007, at 15:55 , Mark Smithfield wrote:
>
>>>> I want to apply smalltalk to fibonacci numbers. I
>>> add
>>>> this method
>>>> to Integer,
>>>>
>>>> fib:
>>>>     (self = 0) ifTrue: (^0)
>>>>     (self = 1) ifTrue: (^1)
>>>>     ^ (self - 1 fib) + (self - 2 fib).
>
> This is not Smalltalk. Blocks need square brackets, and unary  
> messages bind more closely that binary messages (a.k.a. operators):
>
> fib
> 	^self <= 1
> 		ifTrue: [self]
> 		ifFalse: [(self - 1) fib + (self - 2) fib]
>
>>>> Next, I would like to memoize this method,
>>> (because of
>>>> the enormous performance gains).  I do not see how
>>> to
>>>> memo-ize things in smalltalk. Can somebody help me
>>> see the necessary
>>>> shift in thinking?
(Continue reading)

Michael Davies | 3 Oct 17:10 2007
Picon

Re: Re: Beginners Digest, Vol 18, Issue 2

On 03/10/2007, Bert Freudenberg <bert <at> freudenbergs.de> wrote:
>
>
> Now that is plain silly. If you just iterate, why store in an array?

Good question. No good answer :-(
Your solution is >20x faster, thanks!
Chris Cunningham | 3 Oct 18:01 2007
Picon

Re: assorted beginner questions

Hi.

On 10/2/07, Mark Smithfield <m_smithfield <at> yahoo.com> wrote:
As I read code in Squeak, I discover method names that
I do not
know. How do you find these methods if you don't know
the type of
the receiver?
 
If you are asking how to find out what a method does from the code, the easiest way is to place the cursor over the method (just a part of it) and press alt+m (on windows, or maybe ctl+m, or on a Mac, cmd+m).  This should bring up the implementors of the method, and you can browse through all of the implementors and look at the code.  If that still doesn't make sense, you can ask about the method here and others will nicely explain it to you (like Ron did).
 
>> In the floor method from Number,
>>
>>     truncation _ self truncated.
> >
>> What does the underscore mean?
>
>:=
>In some images it shows up as a left-pointing arrow.
>I'm not sure if that's coming back in later iterations or not.
 
The underscore is an assignment.  If you have picked the right font, you won't see underscores in the code, but rather a left-pointing arrow.  I believe these fonts are still in the current versions of Squeak - but they just arent the default in some (most?) of the distributions.  So, if you change the font (or modify the font yourself - I haven't done that, but it is doable), then you can currently get back the left-pointing arrow.
 
-Chris
 

 
_______________________________________________
Beginners mailing list
Beginners <at> lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners

Gmane