w***@gmail.com

2014-11-14 21:31:14 UTC

I created a Rational class. Inside this class, I used a function to reduce numerator and denominator so that the two are essentially prime to each other. I used a block to generate the gcd number. However, it seems that there is an error in the gcd block. Below is part of the code:

Rational extend [

reduce [

gcd := [ :x :y |

(x < y)

ifTrue: [ |temp| temp := x. "make sure x stores larger val"

x := y. "than y"

y := temp. ]

[(y=0) not] whileTrue: [ |r|

r := x \\ y.

x := y.

y := r. ]

^x. "when done, return the gcd"

]

"numer and denom" are defined as instanceVariableNames "

"for simplicity I assume numer and denom are not negative "

numer := numer // (gcd value: numer value: denom).

denom := denom // (gcd value: numer value: denom).

]

The error is in the line : [(y=0) not] whileTrue: [|r|

It says "parse error, expected '[' ". Does someone know what is wrong with the code?

Also, I would like to know if using block this way is correct.

Thank you very much

Rational extend [

reduce [

gcd := [ :x :y |

(x < y)

ifTrue: [ |temp| temp := x. "make sure x stores larger val"

x := y. "than y"

y := temp. ]

[(y=0) not] whileTrue: [ |r|

r := x \\ y.

x := y.

y := r. ]

^x. "when done, return the gcd"

]

"numer and denom" are defined as instanceVariableNames "

"for simplicity I assume numer and denom are not negative "

numer := numer // (gcd value: numer value: denom).

denom := denom // (gcd value: numer value: denom).

]

The error is in the line : [(y=0) not] whileTrue: [|r|

It says "parse error, expected '[' ". Does someone know what is wrong with the code?

Also, I would like to know if using block this way is correct.

Thank you very much