Skip to content

Commit

Permalink
iterative ackermann
Browse files Browse the repository at this point in the history
2007-11-08  Paolo Bonzini  <bonzini@gnu.org>

	* tests/ackermann.st: Add "iterative" implementation.

git-archimport-id: bonzini@gnu.org--2004b/smalltalk--devo--2.2--patch-629
  • Loading branch information
bonzini committed Jan 9, 2008
1 parent d46c38c commit e415a80
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
@@ -1,3 +1,7 @@
2007-11-08 Paolo Bonzini <bonzini@gnu.org>

* tests/ackermann.st: Add "iterative" implementation.

2007-11-07 Paolo Bonzini <bonzini@gnu.org>

* examples/Methods.st: Add brackets around compiled methods.
Expand Down
25 changes: 23 additions & 2 deletions tests/ackermann.st
Expand Up @@ -34,13 +34,34 @@ Integer extend [
n = 0 ifTrue: [ ^self - 1 ack: 1 ].
^self - 1 ack: (self ack: n - 1)
]

ackIt: nn [
| n |
^[
| m stack |
m := self. n := nn.
stack := OrderedCollection new.
[ m = 0
ifTrue: [
n := n + 1.
m := stack removeLast ]
ifFalse: [
n = 0
ifTrue: [
m := m - 1.
n := 1 ]
ifFalse: [
stack addLast: m - 1.
n := n - 1 ] ]
] repeat
] on: Error do: [ :ex | ex return: n ]
]
]

Eval [
n := Smalltalk arguments isEmpty
ifTrue: [ 4 ]
ifFalse: [ Smalltalk arguments first asInteger ].

('Ack(3,%1): %2' % { n. (3 ack: n) }) displayNl

('Ack(3,%1): %2' % { n. (3 ack: n) }) displayNl.
]

0 comments on commit e415a80

Please sign in to comment.