| 1 |
(******************************************************************************** |
|---|
| 2 |
Copyright 2008 Sun Microsystems, Inc., |
|---|
| 3 |
4150 Network Circle, Santa Clara, California 95054, U.S.A. |
|---|
| 4 |
All rights reserved. |
|---|
| 5 |
|
|---|
| 6 |
U.S. Government Rights - Commercial software. |
|---|
| 7 |
Government users are subject to the Sun Microsystems, Inc. standard |
|---|
| 8 |
license agreement and applicable provisions of the FAR and its supplements. |
|---|
| 9 |
|
|---|
| 10 |
Use is subject to license terms. |
|---|
| 11 |
|
|---|
| 12 |
This distribution may include materials developed by third parties. |
|---|
| 13 |
|
|---|
| 14 |
Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered |
|---|
| 15 |
trademarks of Sun Microsystems, Inc. in the U.S. and other countries. |
|---|
| 16 |
********************************************************************************) |
|---|
| 17 |
|
|---|
| 18 |
component atomicList |
|---|
| 19 |
export Executable |
|---|
| 20 |
|
|---|
| 21 |
trait List[\E\] comprises {Elem[\E\], Empty[\E\]} |
|---|
| 22 |
car():E |
|---|
| 23 |
cdr():List[\E\] |
|---|
| 24 |
cons(x:E):List[\E\] |
|---|
| 25 |
isEmpty():Boolean |
|---|
| 26 |
toString():() |
|---|
| 27 |
count():ZZ32 |
|---|
| 28 |
end |
|---|
| 29 |
|
|---|
| 30 |
object ListError(msg:String) extends Exception end |
|---|
| 31 |
|
|---|
| 32 |
object Empty[\E\] extends List[\E\] |
|---|
| 33 |
car():E = throw ListError("Empty List has no car") |
|---|
| 34 |
cdr():List[\E\] = throw ListError(" Empty List has no cdr") |
|---|
| 35 |
cons(z:E) = Elem[\E\](z, self) |
|---|
| 36 |
toString() = " Empty" |
|---|
| 37 |
isEmpty() = true |
|---|
| 38 |
count() = 0 |
|---|
| 39 |
end |
|---|
| 40 |
|
|---|
| 41 |
object Elem[\E\](val:E, rest:List[\E\]) extends List[\E\] |
|---|
| 42 |
car():E = val |
|---|
| 43 |
cdr():List[\E\] = rest |
|---|
| 44 |
cons(z:E) = Elem[\E\](z,self) |
|---|
| 45 |
toString() = " " val ", " rest.toString() |
|---|
| 46 |
isEmpty() = false |
|---|
| 47 |
count() = 1 + cdr().count() |
|---|
| 48 |
end |
|---|
| 49 |
|
|---|
| 50 |
run(args:String...):()=do |
|---|
| 51 |
init:List[\ZZ32\] := Empty[\ZZ32\] |
|---|
| 52 |
for i<-1#10 do |
|---|
| 53 |
atomic do |
|---|
| 54 |
init := init.cons(i) |
|---|
| 55 |
end |
|---|
| 56 |
end |
|---|
| 57 |
if init.count() = 10 then println("Pass") else println("Fail") end |
|---|
| 58 |
|
|---|
| 59 |
end |
|---|
| 60 |
end |
|---|