home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / abc / abcintro.doc < prev    next >
Text File  |  1991-04-22  |  9KB  |  306 lines

  1. A SHORT INTRODUCTION TO THE ABC LANGUAGE
  2.  
  3. This article gives a quick overview of the programming language ABC
  4. and its implementations, and gives a few examples of ABC programs.
  5. Full documentation about ABC is in the ABC Programmer's Handbook
  6. (details below).
  7.  
  8. THE LANGUAGE
  9. ABC is an imperative language originally designed as a replacement for
  10. BASIC: interactive, very easy to learn, but structured, high-level,
  11. and easy to use. ABC has been designed iteratively, and the present
  12. version is the 4th iteration. The previous versions were called B (not
  13. to be confused with the predecessor of C).
  14.  
  15. It is suitable for general everyday programming, the sort of
  16. programming that you would use BASIC, Pascal, or AWK for. It is not a
  17. systems-programming language. It is an excellent teaching language,
  18. and because it is interactive, excellent for prototyping. It is much
  19. faster than Unix 'bc' for doing quick calculations.
  20.  
  21. ABC programs are typically very compact, around a quarter to a fifth
  22. the size of the equivalent Pascal or C program. However, this is not
  23. at the cost of readability, on the contrary in fact (see the examples
  24. below).
  25.  
  26. ABC is simple to learn due to the small number of types in the
  27. language (five). If you already know Pascal or something similar you
  28. can learn the whole language in an hour or so.  It is easy to use
  29. because the data-types are very high-level.
  30.  
  31. The five types are:
  32.    numbers: unbounded length, with exact arithmetic the rule
  33.    texts (strings): also unbounded length
  34.    compounds: records without field names
  35.    lists: sorted collections of any one type of items (bags or multi-sets)
  36.    tables: generalised arrays with any one type of keys, any one type
  37.        of items (finite mappings).
  38.  
  39. THE ENVIRONMENT
  40. The implementation includes a programming environment that makes
  41. producing programs very much easier, since it knows a lot about the
  42. language, and can therefore do much of the work for you. For instance,
  43. if you type a W, the system suggests a command completion for you:
  44.     W?RITE ?
  45.  
  46. If that is what you want, you press [tab], and carry on typing the
  47. expression; if you wanted WHILE, you type an H, and the system changes
  48. the suggestion to match:
  49.     WH?ILE ?:
  50.  
  51. This mechanism works for commands you define yourself too. Similarly,
  52. if you type an open bracket or quote, you get the closing bracket or
  53. quote for free. You can ignore the suggestions if you want, and just
  54. type the commands full out.
  55.  
  56. There is support for workspaces for developing different programs.
  57. Within each workspace variables are persistent, so that if you stop
  58. using ABC and come back later, your variables are still there as you
  59. left them. This obviates the need for file-handling facilities: there
  60. is no conceptual difference between a variable and a file in ABC.
  61.  
  62. The language is strongly-typed, but without declarations. Types are
  63. determined from context.
  64.  
  65. EXAMPLES
  66. The (second) best way to appreciate the power of ABC is to see some
  67. examples (the first is to use it). In what follows, >>> is the
  68. prompt from ABC:
  69.  
  70. NUMBERS
  71.     >>> WRITE 2**1000
  72.     107150860718626732094842504906000181056140481170553360744375038837
  73.     035105112493612249319837881569585812759467291755314682518714528569
  74.     231404359845775746985748039345677748242309854210746050623711418779
  75.     541821530464749835819412673987675591655439460770629145711964776865
  76.     42167660429831652624386837205668069376
  77.  
  78.     >>> PUT 1/(2**1000) IN x
  79.     >>> WRITE 1 + 1/x
  80.     107150860718626732094842504906000181056140481170553360744375038837
  81.     035105112493612249319837881569585812759467291755314682518714528569
  82.     231404359845775746985748039345677748242309854210746050623711418779
  83.     541821530464749835819412673987675591655439460770629145711964776865
  84.     42167660429831652624386837205668069377
  85.  
  86. TEXTS
  87.     >>> PUT ("ha " ^^ 3) ^ ("ho " ^^ 3) IN laugh
  88.     >>> WRITE laugh
  89.     ha ha ha ho ho ho 
  90.  
  91.     >>> WRITE #laugh
  92.     18
  93.  
  94.     >>> PUT "Hello! "^^1000 IN greeting
  95.     >>> WRITE #greeting
  96.     7000
  97.  
  98. LISTS
  99.     >>> WRITE {1..10}
  100.     {1; 2; 3; 4; 5; 6; 7; 8; 9; 10}
  101.     >>> PUT {1..10} IN l
  102.     >>> REMOVE 5 FROM l
  103.     >>> INSERT 4 IN l
  104.     >>> INSERT pi IN l
  105.     >>> WRITE l
  106.     {1; 2; 3; 3.141592653589793; 4; 4; 6; 7; 8; 9; 10}
  107.  
  108.     >>> PUT {} IN ll
  109.     >>> FOR i IN {1..3}:
  110.             INSERT {1..i} IN ll
  111.     >>> WRITE ll
  112.     {{1}; {1; 2}; {1; 2; 3}}
  113.     >>> FOR l IN ll:
  114.             WRITE l /
  115.     {1}
  116.     {1; 2}
  117.     {1; 2; 3}
  118.     >>> WRITE #ll
  119.     3
  120.  
  121. COMPOUNDS
  122.     >>> PUT ("Square root of 2", root 2) IN c
  123.     >>> WRITE c
  124.     ("Square root of 2", 1.414213562373095)
  125.     >>> PUT c IN name, value
  126.     >>> WRITE name
  127.     Square root of 2
  128.     >>> WRITE value
  129.     1.414213562373095
  130.  
  131. A TELEPHONE LIST
  132. This uses the table data-type. In use, tables resemble arrays:
  133.  
  134.     >>> PUT {} IN tel
  135.     >>> PUT 4054 IN tel["Jennifer"]
  136.     >>> PUT 4098 IN tel["Timo"]
  137.     >>> PUT 4134 IN tel["Guido"]
  138.  
  139.     >>> WRITE tel["Jennifer"]
  140.     4054
  141.  
  142. You can write all ABC values out. Tables are kept sorted on the keys:
  143.     >>> WRITE tel
  144.     {["Guido"]: 4134; ["Jennifer"]: 4054; ["Timo"]: 4098}
  145.  
  146. The keys function returns a list:
  147.     >>> WRITE keys tel
  148.     {"Guido"; "Jennifer"; "Timo"}
  149.  
  150.     >>> FOR name IN keys tel:
  151.            WRITE name, ":", tel[name] /
  152.     Guido: 4134
  153.     Jennifer: 4054
  154.     Timo: 4098
  155.  
  156. You can define your own commands:
  157.  
  158.     HOW TO DISPLAY t:
  159.        FOR name IN keys t:
  160.           WRITE name<<10, t[name] /
  161.  
  162.     >>> DISPLAY tel
  163.     Guido      4134
  164.     Jennifer   4054
  165.     Timo       4098
  166.  
  167. To find the user of a given number, you can use a quantifier:
  168.     >>> IF SOME name IN keys tel HAS tel[name] = 4054:
  169.            WRITE name
  170.     Jennifer
  171.  
  172. Or create the inverse table:
  173.     >>> PUT {} IN subscriber
  174.     >>> FOR name IN keys tel:
  175.            PUT name IN subscriber[tel[name]]
  176.  
  177.     >>> WRITE subscriber[4054]
  178.     Jennifer
  179.  
  180.     >>> WRITE subscriber
  181.     {[4054]: "Jennifer"; [4098]: "Timo"; [4134]: "Guido"}
  182.  
  183. Commands and functions are polymorphic:
  184.     >>> DISPLAY subscriber
  185.     4054       Jennifer
  186.     4098       Timo
  187.     4134       Guido
  188.  
  189. Functions may return any type. Note that indentation is significant -
  190. there are no BEGIN-END's or { }'s:
  191.  
  192.     HOW TO RETURN inverse t:
  193.        PUT {} IN inv
  194.        FOR k IN keys t:
  195.           PUT k IN inv[t[k]]
  196.        RETURN inv
  197.  
  198.     >>> WRITE inverse tel
  199.     {[4054]: "Jennifer"; [4098]: "Timo"; [4134]: "Guido"}
  200.  
  201.     >>> DISPLAY inverse inverse tel
  202.     Guido      4134
  203.     Jennifer   4054
  204.     Timo       4098
  205.  
  206. A CROSS-REFERENCE INDEXER
  207.  
  208. 'Text files' are represented as tables of numbers to strings:
  209.  
  210.     >>> DISPLAY poem
  211.     1         I've never seen a purple cow
  212.     2         I hope I never see one
  213.     3         But I can tell you anyhow
  214.     4         I'd rather see than be one
  215.  
  216. The following function takes such a document, and returns the
  217. cross-reference index of the document: a table from words to lists of
  218. line-numbers:
  219.  
  220.     HOW TO RETURN index doc:
  221.        PUT {} IN where
  222.        FOR line.no IN keys doc:
  223.           TREAT LINE
  224.        RETURN where
  225.     TREAT LINE:
  226.        FOR word IN split doc[line.no]:
  227.           IF word not.in keys where:
  228.          PUT {} IN where[word]
  229.           INSERT line.no IN where[word]
  230.  
  231. TREAT LINE here is a refinement, directly supporting
  232. stepwise-refinement. 'split' is a function that splits a string into
  233. its space-separated words:
  234.  
  235.     >>> WRITE split "Hello world"
  236.     {[1]: "Hello"; [2]: "world"}
  237.  
  238.     >>> DISPLAY index poem
  239.     But        {3}
  240.     I          {2; 2; 3}
  241.     I'd        {4}
  242.     I've       {1}
  243.     a          {1}
  244.     anyhow     {3}
  245.     be         {4}
  246.     can        {3}
  247.     cow        {1}
  248.     hope       {2}
  249.     never      {1; 2}
  250.     one        {2; 4}
  251.     purple     {1}
  252.     rather     {4}
  253.     see        {2; 4}
  254.     seen       {1}
  255.     tell       {3}
  256.     than       {4}
  257.     you        {3}
  258.  
  259. MORE INFORMATION
  260. Full details of ABC and the implementations, along with many example
  261. programs are in the book "The ABC Programmer's Handbook" by Leo Geurts,
  262. Lambert Meertens and Steven Pemberton, published by Prentice-Hall
  263. (ISBN 0-13-000027-2).
  264.  
  265. See also Steven Pemberton, "An Alternative Simple Language and
  266. Environment for PCs", IEEE Software, Vol. 4, No. 1, January 1987, pp.
  267. 56-64.
  268.  
  269. There is an irregular newsletter available from us (address below),
  270. and a mailing list for discussions; to join send your preferred email
  271. address to abc-list-request@cwi.nl .
  272.  
  273. IMPLEMENTATIONS
  274. The sources for the Unix version have been posted to the
  275. comp.sources.unix group on Usenet; the binaries to comp.binaries.{mac,
  276. ibm.pc, atari.st}. They are also available from some servers, for
  277. instance by anonymous ftp from hp4nl.nluug.nl [192.16.202.2],
  278. mcsun.eu.net [192.16.202.1], and uunet.uu.net [192.48.96.2], in the
  279. directory {pub}/{programming}/languages/abc, or send the mail message
  280.     request: programming/languages/abc
  281.     topic: index
  282. to info-server@hp4nl.nluug.nl, for a list of the available files, or use
  283.     topic: <filename>
  284. to get one of the files.
  285.  
  286. As of this writing, the available files are:
  287.  
  288.     index        for a list of all files available
  289.     abc.intro    for an overview of ABC
  290.             (also included with the implementations below)
  291.     abcst.arc    for the Atari ST version
  292.     abcpc.arc    for the IBM PC version
  293.     abc.mac.sit.hqx    for the Mac version
  294.     abc.unix.tar.Z    for the Unix version
  295.     README        for an explanation of how to unpack the above files
  296.  
  297. ADDRESS
  298.     ABC Implementations
  299.     CWI/AA
  300.     Kruislaan 413
  301.     1098 SJ AMSTERDAM
  302.     The Netherlands
  303.  
  304.     Email: abc@cwi.nl
  305.  
  306.