ABC: Some Simple Examples

The (second) best way to appreciate the power of ABC is to see some examples (the first is to use it). In what follows, >>> is the prompt from ABC.

There are five types in the language: two basic -- Numbers and Texts -- and three structured -- Compounds, Lists, and Tables.

Numbers

Numbers are unbounded, and stored exact:
 >>> WRITE 2**1000
 107150860718626732094842504906000181056140481170553360744375038837
 035105112493612249319837881569585812759467291755314682518714528569
 231404359845775746985748039345677748242309854210746050623711418779
 541821530464749835819412673987675591655439460770629145711964776865
 42167660429831652624386837205668069376
 >>> PUT 1/(2**1000) IN x
 >>> WRITE 1 + 1/x
 107150860718626732094842504906000181056140481170553360744375038837
 035105112493612249319837881569585812759467291755314682518714528569
 231404359845775746985748039345677748242309854210746050623711418779
 541821530464749835819412673987675591655439460770629145711964776865
 42167660429831652624386837205668069377
Non-exact numbers use machine-precision:
 >>> WRITE root 2
 1.414213562373095

Texts

Texts (strings of characters) are also unbounded:
 >>> PUT ("ha " ^^ 3) ^ ("ho " ^^ 3) IN laugh
 >>> WRITE laugh
 ha ha ha ho ho ho 
 >>> WRITE #laugh
 18
 >>> PUT "Hello! "^^1000 IN greeting
 >>> WRITE #greeting
 7000
 >>> WRITE greeting|4
 Hell
 >>> WRITE greeting@4|3
 lo!

Lists

Lists are sorted lists of values of any one other type:
 >>> WRITE {1..10}
 {1; 2; 3; 4; 5; 6; 7; 8; 9; 10}
 >>> PUT {1..10} IN l
 >>> REMOVE 5 FROM l
 >>> INSERT 4 IN l
 >>> INSERT pi IN l
 >>> WRITE l
 {1; 2; 3; 3.141592653589793; 4; 4; 6; 7; 8; 9; 10}
You can have lists of any type, so here is a list of lists:
 >>> PUT {} IN ll
 >>> FOR i IN {1..3}:
 INSERT {1..i} IN ll
 >>> WRITE ll
 {{1}; {1; 2}; {1; 2; 3}}
 >>> FOR l IN ll:
 WRITE l /
 {1}
 {1; 2}
 {1; 2; 3}
 >>> WRITE #ll
 3

Compounds

Compounds are like records or structures, but without field names:
 >>> PUT ("Square root of 2", root 2) IN c
 >>> WRITE c
 ("Square root of 2", 1.414213562373095)
 >>> PUT c IN name, value
 >>> WRITE name
 Square root of 2
 >>> WRITE value
 1.414213562373095

Tables

Tables resemble arrays:
 >>> PUT {} IN tel
 >>> PUT 4054 IN tel["Jennifer"]
 >>> PUT 4098 IN tel["Timo"]
 >>> PUT 4134 IN tel["Guido"]
 >>> WRITE tel["Jennifer"]
 4054
You can write all ABC values out. Tables are kept sorted on the keys:
 >>> WRITE tel
 {["Guido"]: 4134; ["Jennifer"]: 4054; ["Timo"]: 4098}
The keys function returns a list:
 >>> WRITE keys tel
 {"Guido"; "Jennifer"; "Timo"}
 >>> FOR name IN keys tel:
 WRITE name, ":", tel[name] /
 Guido: 4134
 Jennifer: 4054
 Timo: 4098
Some complete example programs

AltStyle によって変換されたページ (->オリジナル) /