Hi,
I'm fairly new to Squeak and Smalltalk, I've been playing with various programming snippets to improve my fluency.
I have some Ruby code that generates an Ascii representation of a Mandelbrot fractal, and thought it may be fun to translate over to squeak.
I've managed to tie myself in knots with a basic syntax error, and I can't spot it - can anyone mentor me and point me towards the error? It's driving me nuts :-)
Here's the code:
createFractal "Generate a MandelBrot image using only ASCII chars" | d e b r n h | e := 1.1. (e > -1.2) whileTrue: [ b := -2.0. (b < 1.0) whileTrue: [r := 0.0. n := 0.0. h := 127. ((r*r + n*n < 4.0) and: [h > 32]) whileTrue: [h := h -1. d := r. r := r*r-n*n+b. n := 2*d*n+e. ]. b := b + 0.04. Transcript show: (Character value: ((b > 1) ifTrue: [10] ifFalse: [h]) ) ]. e := e - 0.1 . ]
The original Ruby code is:
#!/usr/bin/ruby
e = 1.1 while e > -1.2 b = -2.0 while b < 1.0 r = n = 0.0 h = 127 while (r*r + n*n < 4.0) and ( (h-=1) > 32) d = r r = r*r-n*n+b n = 2*d*n+e end b += 0.04 putc b>1 ? 10 : h end e -= 0.1 End
Thanks in advance!
Chris
On Oct 26, 2004, at 1:26 PM, Christopher Barham wrote:
Hi,
I'm fairly new to Squeak and Smalltalk, I've been playing with various programming snippets to improve my fluency.
I have some Ruby code that generates an Ascii representation of a Mandelbrot fractal, and thought it may be fun to translate over to squeak.
I've managed to tie myself in knots with a basic syntax error, and I can't spot it - can anyone mentor me and point me towards the error? It's driving me nuts :-)
Well, your "syntax" error (or rather, the semantic error that happens to be caught by the compiler) is that you're sending #whileTrue: to something that isn't a block - it should be
[e > -1.2] whileTrue: [...]
rather than
(e > -1.2) whileTrue: [...]
and ditto elsewhere in your code.
Intuitively, you need this because the comparison of e with -1.2 is going to be evaluated many times, and so it needs to be inside a block to allow that to happen. That's why #whileTrue: is sent to a block, but #ifTrue: isn't.
Looking ahead a couple of steps, let me just point out that Smalltalk doesn't special case the precedence of arithmetic operators (they're left to right like any other selector), so you may want to add in a few parentheses here and there.
Avi
squeak-dev@lists.squeakfoundation.org