Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
ALGOL 68
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===struct, union & {{code|[:]}}: Structures, unions and arrays=== ALGOL 68 supports [[Array data structure|arrays]] with any number of dimensions, and it allows for the ''slicing'' of whole or partial rows or columns. '''MODE''' '''VECTOR''' = [1:3] '''REAL'''; ''# vector '''MODE''' declaration (typedef) #'' '''MODE''' '''MATRIX''' = [1:3,1:3]'''REAL'''; ''# matrix '''MODE''' declaration (typedef) #'' '''VECTOR''' v1 := (1,2,3); ''# array variable initially (1,2,3) #'' []'''REAL''' v2 = (4,5,6); ''# constant array, type equivalent to '''VECTOR''', bounds are implied #'' '''OP''' + = ('''VECTOR''' a,b) '''VECTOR''': ''# binary '''OP'''erator definition #'' ('''VECTOR''' out; '''FOR''' i '''FROM''' βa '''TO''' βa '''DO''' out[i] := a[i]+b[i] '''OD'''; out); '''MATRIX''' m := (v1, v2, v1+v2); print ((m[,2:])); ''# a slice of the 2nd and 3rd columns #'' Matrices can be sliced either way, e.g.: '''REF''' '''VECTOR''' row = m[2,]; ''# define a '''REF''' (pointer) to the 2nd row #'' '''REF''' '''VECTOR''' col = m[,2]; ''# define a '''REF''' (pointer) to the 2nd column #'' ALGOL 68 supports multiple field structures ('''STRUCT''') and '''[[united modes]]'''. Reference variables may point to any '''MODE''' including array slices and structure fields. For an example of all this, here is the traditional linked list declaration: '''MODE''' '''NODE''' = '''UNION''' ('''VOID''', '''REAL''', '''INT''', '''COMPL''', '''STRING'''), '''LIST''' = '''STRUCT''' ('''NODE''' val, '''REF''' '''LIST''' next); Usage example for '''UNION''' '''CASE''' of '''''NODE''''': {| border="1" style="border-collapse:collapse; border:5px double gray; float:left;" class="wikitable" ! Algol68<sup>[[#The language of the unrevised report|r0]]</sup> as in the 1968 Final Report ! Algol68<sup>[[#Revisions|r1]]</sup> as in the 1973 Revised Report |- | '''NODE''' n := "1234"; '''REAL''' r; '''INT''' i; '''COMPL''' c; '''STRING''' s '''CASE''' r,i,c,s::=n '''IN''' print(("real:", r)), print(("int:", i)), print(("compl:", c)), print(("string:", s)) '''OUT''' print(("?:", n)) '''ESAC''' | '''NODE''' n := "1234"; # or n := EMPTY; # '''CASE''' n '''IN''' ('''VOID'''): print(("void:", "EMPTY")), ('''REAL''' r): print(("real:", r)), ('''INT''' i): print(("int:", i)), ('''COMPL''' c): print(("compl:", c)), ('''STRING''' s): print(("string:", s)) '''OUT''' print(("?:", n)) '''ESAC''' |} {{clear}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)