Основные элементы языка программирования Icon: Учебное пособие

  • Published on
    08-Dec-2016

  • View
    220

  • Download
    7

Embed Size (px)

Transcript

  • . ..

    ..

    Icon

    - 2004

    2

    519.682 32.973-01873

    188

    -

    :

    . .-. , .. , . .-. , ..

    .. 188 Icon:

    . : - , 2004. 55 . ISBN 5-7779-0523-4 ,

    , -- Icon. - Thomas W. Christopher Icon Programming Language Hand-book, . ; .

    .

    519.682 32.973-01873

    .., 2004 ISBN 5-7779-0523-4 , 2004

  • 3

    ......................................................................................................... 5 1. , , ............................................ 11 2.

    2.1. Every, to, every do............................................................................. 13 2.2. !e ................................................................. 15 2.3. & and ............................................................................. 15 2.4. Backtracking .................................................................... 15 2.5. &null ............................................................................ 16 2.6. | .................................................................................. 16 2.7. .

    .................................................................................... 17 2.8. Coevaluation ..................................................... 17 2.9. ............... 18

    3. 3.1. if......................................................................................................... 19 3.2. case of ................................................................................................ 19 3.3. while do ............................................................................................. 19 3.4. until do ............................................................................................... 20 3.5. repeat . ............................................ 20

    4. 4.1. ............................................... 21 4.2. ? any ..................... 21

    5. ..................................................................................................... 22 5.1. ......................................................................................... 22 5.2. ............................................... 23 5.3. ............................................... 23 5.4. ......................................................... 24 5.5. ......................... 27

    6. 6.1. ............................................................................. 29 6.2. ........................... 29 6.3. ..................................................................... 29 6.4. .................................................. 30 6.5. ........................................................ 30

    7. ................................................................................................... 31 7.1. .......................................................................... 32 7.3. ............................................................ 33

    8. ............................................................................................... 34 9. ....................................................................................................... 35

    4

    10. 10.1. ............................................................................. 36 10.2. ...................................................................... 38

    11. ..................................... 39 12. coexpression .......................................... 41 13.

    13.1. ............................................................................... 44 13.2. ................................................ 46 13.3. ........................................................................................... 47 13.4. ............................................................................... 48 13.5. ...................................................... 50

    .................................................................................................. 51 ........................................................ 52 .......................................................... 54

  • 5

    , -

    [1], . : - - . , , , , - . - , - , , .

    Icon, , - SNOBOL, 60-. , iconoclastic () [1], .

    Icon. -, , - . Icon . , , - -, . . , , , - .

    , - Icon , - . , , -. Christopher [2] : Icon' , -

    6

    Icon' . - , .

    : procedure main() L:=["mmm","nnnnn","oooooo"] # every writes(" ",! L) end : mmm nnnnn oooooo Icon ( -

    , ). , . .

    Icon : - , , , 2 (- ), . - -. , . - . . - . . (- (?)).

    Icon' (first class values, - , ).

    Icon (co-expression) , , - .

    Icon . Icon - -

    . ,

    Icon' , . : Prolog'?! ( Icon ( !) -

  • 7

    (exe ), - PDC Visual Prolog.)

    : procedure main() # f:=open("x.txt","r")|stop("cannot open x.txt") f2:=open("y.txt","w")|stop("cannot open y.txt") # while s:=read(f) do write(f2,s) close(f) close(f2) end Icon' -

    every write(f2,!f) link graphics procedure main() # : WOpen("size=400,300") | stop("can't open window") Font("Times New Roman,18") # L:=[" 1"," 2"," 3"] case SelectDialog(" ",L,L[1]) of { "Okey": { GotoRC(1,1) WWrite(dialog_value) # WDone() } "Cancel": {} } end , , Icon',

    (- ). Cristophera [2, . 41]:

    8

    "The vertical bar, "|", read "or", looks like a binary operator but does not behave like one".

    : "\" "\x" "|(z to y)\x" ( , x, - ).

    Icon . f:=open(__,rt)

    icon_prog.exe __ procedure main(args)) f:=open(args[1],rt) . end , -

    .

    , -:

    allRus:=''

    : allLetters:= &ascii++allRus ..

    Icon ( - ). , . - . .

    : * / % (5%2=1) + -

  • 9

    , (*), - , .

    , http://www.cs.arizona.edu/icon. - Icon ( !). ( .icn) .

    , icn-, -:

    wicont c .icn : wicont .icn icn- procedure main() -

    link : link .icn link .icn . procedure main() end procedure p1() end procedure pn() end pdf-

    http://www.cs.arizona.edu/icon/books.htm. - -, . [2], : online- Icon Programming Language Handbook ( pdf-) Icon

    10

    Idol (- Icon. ..), - . -, "" , - - .

  • 11

    1. , ,

    , , Icon . - , (;). :

    every {writes(" ",1 to 5);writes(" ",6 to 10)} every { writes(" ",1 to 5) writes(" ",6 to 10) }

    . , , if {} then {} else .

    [] , , (L[3] ). coavaluation. - procedure, main(...), (p1,...,pn) .

    "end". . - .

    , . , - (naMe name, Name ).

    local , - - (local x,y).

    static , - - (static x,y).

    12

    global , (global x,y).

    procedure -.

    record () , (record r(x,y,z)).

    link , .

    invocable - - ("f"(x)).

    global, record, link, invocable ( ), - .

    , , Christopher', - e ei, i , .

  • 13

    2.

    ( ), - . -, , :

    -, - ;

    -, , -, , .

    . - , - . , . , . , , , , p:

    p:-fact1(_,_,X,_),fact2(_,Y,_), X

  • 15

    2.2. !e

    e , !e , - .

    : L:=[1,2];every writes(" ",!L).#: 1 2 s:="cat";every writes(" ",!s).#: c a t , : every writes(" ",5 to 6)+(10 to 20)) 15 25 16 26 2.3. & and

    e1&e2

    . every i:=|seq(1,2)\3 & j:=|seq(2,2)\3 & i~= j & writes(" ",i+j) : 3 5 7 5 7 9 7 9 11 2.4. Backtracking

    , , . , - 12345 ( write("12345")): every writes (1 to 5).

    , Icon &fail, -.

    write (1 to 5) + &fail. write("\n","O'key") +

    , . . "-".

    : procedure main() S:="tak pit hochetsa, chto perenochevat negde\n" (sim:=!S, writes(sim),sim=="\n") write("O'key")

    16

    end : S O'key .

    , - (sim:=!S) -. S, O'key.

    2.5. &null

    \ , -, &null, / , . . -

    if \x & \y then...". x , if

    ~x, if /x . 2.6. |

    |, , , . -:

    e1 | e2 e1, e2.

    procedure main() local x,y,z L:=["Vera","Manja","Tanja"] x:=1 if (\x | \y) then z:=10 (writes(" ",!L) | write(" ",z)) every (writes(" ",!L) | write(" ",z)) #while (writes(" ",!L)) | write(" ",z)) end : Vera Vera Manja Tanja 10 end,

    Vera.

  • 17

    2.7. . -

    |e e, - .

    , - : |e\n.

    : every |(writes(7))\7 7777777, , every (writes(|7\7)), every i:=1 to 7 do writes(7). seq()

    seq() 1,2,3,............. seq(i) i,i+1,i+2........ seq(i,j) i+j,i+2j,i+3j.. every writes(" ",seq(2,2)\5) 2 4 6 8 10 every writes(" ",seq(2,2))\5 2.8. Coevaluation

    :

    e0(e1,e2,...,eN),

    e0 , -, , , - , - . .

    : (e1,e2,...,eN) N(e1,e2,...,eN) (e1&e2&...&eN). : procedure main() K:=1 S:="qwertyu" every writes(" ",K(!S,1 to 3)) write()

    18

    every writes(" ",(!S,1 to 3)) write() every writes(" ",(!S & 1 to 3)) end : q q q w w w e e e r r r t t t y y y u u u 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 2.9. -

    , a

  • 19

    3.

    3.1. if...

    if e1 then e2 else e3 if e1 then e2 e1 e3 ,

    . 3.2. case of

    case e1 of { e2 : e3

    e4 : e5 ... e2n : e2n+1 default : e2n+2 } 3.3. while do

    while e1 do e2 while e1 e1, , -

    e2. : i:=0 while i100 do {write(i,j);i+:=j;i:=:j} end 3.5. repeat .

    i:=1 repeat { if i

  • 21

    4. : 'abcd'.

    Icon' : &ascii 128 ASCII. &cset 256 . &digits ('0123456789'). &lcase . &ucase . &letters ( &lcase

    &ucase). 4.1.

    -c cset, , c.

    *c c. c1**c2 c1 c2. c1++c2 c1 c2. c1--c2 c1 c2.

    c1, c2. 4.2. ? any

    any(c,s) 2, s[1] - c.

    any(c,s,i) i+1, s[i] c.

    22

    5. S:="This is string" i- , -

    s:=S[i]. - , , :

    S[1]:="t" . "This is string"[1]:="t" . . procedure main() S:="This is string" writes(S[1]) writes(" ",S[14]) writes(" ",S[-1]) end : T g g 5.1.

    , :

    , , + 1;

    , 0 , -(n+1) (n- ).

    . S:="This is string" write(S[9:15])# string. write(S[0:-6])# string. write(S[15:9])# string. # , . write(S[2:0])# his is string write(S[-1:1])# This is strin write(S[9:12])" str

  • 23

    , , - , :

    S[9:15]:="xxxxxx" , "This is string"[9:15]:="xxxxxxx". S[i+:j] S[i:i+j], S[i-:j] S[i:i-j], 5.2.

    procedure main() s1:="String 1" s2:="String 2" s3:="String 3" s4:="String 2" s5:="STRING 2" s7:="string string string" write(s1||" "||s2||" "||s3||" "||s4||" "||"STRING 2") if s1==s2 then write("s1==s2") # if s1==s3 then write("s1==s3") # if s2==s4 then write("s2==s4") # if s1s2 then write("s1s2") # # if s1s2 then write("s1s2") # if s5s4 then write(s5s4) # every write(" ",!s3) # ! every write(s3[1 to *s3]) # write(?s3) # ?s3 end 5.3.

    center(s,i) i, i>*s, i . -.

    center(s,i,s2) i - s2.

    char(i) , - ( 0 255).

    map(s) .

    24

    map(s1,s2,s3) s1, s2 j, s3[j].

    mapstrs(s,L1,L2) , L1 L2 -. s, L1, - L2. ( mapstrs Icon', . . link mapstrs).

    replace(s1,s2,s3) s2 s1 s3. repl(s,i) i s: s||s...||s. revers(s) s. string(x) (cset)

    . trim(s) s. 5.4.

    : function(x,s,i,j), x , , s -

    , i , j .

    -, .

    any(c,s) 2, s[1] (cset) , .

    any(c,s,i) i+1, s[i] (cset) c, .

    any(c,s,i,j) i+1 (i

  • 25

    many(c,s) , s, - c. *s+1, s c. , s[1] c.

    many(c,s,i) , i. many(c,s,i,j) ,

    s[i:j] ( s[1],s[2],,s[j-1]). s:=abcd; write(many(&lcase,s)) : 5 s:=abcD; write(many(&lcase,s)) : 4 s:=Dabcd; if many(&lcase,s) then write(OKEY) else write(NO!!) : NO!! match(s1,s2) *s1+1, s2[1:*s1+1]==s1 match(s1,s2,i) *s1+I, s2[i:*s1+i]==s1 match(s1,s2,i,j) , j

    . upto(c,s) s 1 to *s, -

    c. : s:=1D2a3b4c5d every writes(upto(&digits,s)) 13579 upto(c,s,i) , s i. upto(c,s,i,j) s i -

    j. [3] ,

    , , ( - ):

    #generate identifiers in string

    26

    procedure idents(s) local i, j, initIdChars, idChars initIdChars := &letters++_ idChars := initIdChars++&digits i := 1 while j := upto(initIdChars,s,i) & not any(idChars,s[j-1]) & k := many(idChars,s,j) do { suspend s[j:k] i:=k } end ,

    : global allRus procedure main() allRus:='

    ' s:=" -

    : 17 _85 56" s1:="a111_d t75" my_file:=open("idents.txt","wt") every write(my_file,idents(s)) end procedure idents(s) local i, j, initIdChars, idChars initIdChars := &letters++'_'++allRus idChars := initIdChars++&digits s ? suspend tab(upto(initIdChars)) & pos(1) | (move(-1),tab(any(~idChars))) & tab(many(idChars)) end : -

    _85 56

  • 27

    5.5.

    any, many, match, find , - , - .

    - ?, s ? e, s , e .

    e, Icon - &subject &pos.

    Icon : s. &subject &pos. &subject s, &pos 1. e. 2 &subject

    &pos. e. ?

    &pos tab(i) move(j). &pos i, - &pos+j. .

    : procedure main() s:="aaabbbccc" s ? write(tab(4)) end : aaa procedure main() s:="aaabbbccc" s ? write(move(4)) end : aaab procedure main() s:="aaabbbccc"

    28

    s ? {writes(tab(4));writes(move(4))} end : aaa bbbc

    [3, . 78]:

    #generate identifiers in string procedure idents(s) local i, j, initIdChars, idChars initIdChars := &letters++_ idChars := initIdChars++&digits s ? suspend tab(upto(initIdChars)) & pos(1) | (move(-1),tab(any(~idChars))) & tab(many(idChars)) end

  • 29

    6. , -

    . - , Icon', .

    6.1.

    : L:=[] . L:=list() . L:=[e1,e2,...,eN] N . L:=list(N) N , -

    &null ( , - ).

    L:=list(N,Val) N , Val.

    6.2.

    .

    L[1], L[2], ...L[*L] 1- . L[5][2] L. L[3:5] (3- 4-). 6.3.

    !L . :

    ?L . L ||| M . L === M ,

    . L ~=== M , -

    .

    30

    6.4.

    copy(L) L (L1:=copy(L)). set(L) (seT:=set(L)). sort(L) .

    , - .

    sortf(L,i) . i i- .

    : procedure main() L:=[[1,7,5],[7,6,9],[11,67,7]] every L1:=!sortf(L,2) do every write(" ",!L1) end : 7 6 9 1 7 5 11 67 7 6.5.

    .

    get(L) . pop(L) . pull(L) . push(L,x) x L 1- ,

    . push(L,x1,x2,...,xN) -

    push . put(L,x),put(L,x1,x2,...,xN) -

    . : L M: every

    put(L,!M).

  • 31

    7. ,

    -. - . -: , , .

    : t:=table() : t[k]:=v : \t[k] , k . /t[k] , k . .

    . , .

    link graphics procedure main() tab1:=table() names_obj_table1:=["",""] listEl:=[["","",""],["","","

    "]] every i:=1 to *names_obj_table1 do { tab1[names_obj_table1[i]]:=list() tab1[names_obj_table1[i]]:=listEl[i] } w:=WOpen("size=400,30...

Recommended

View more >