Скачать презентацию Rebol Just a quick look Philosophy n Скачать презентацию Rebol Just a quick look Philosophy n

ee90546a860980acd0cc40a5bc1be097.ppt

  • Количество слайдов: 18

Rebol Just a quick look Rebol Just a quick look

Philosophy n n n Rebol tries to be very simple to use, especially at Philosophy n n n Rebol tries to be very simple to use, especially at first Syntax is very simple, largely intuitive Vocabulary can be quite large Rebol depends on “dialects”: special-purpose vocabularies for different kinds of tasks Rebol is described as a “messaging language, ” and has great support for web and email Data, including sounds and images, is represented as characters

Elements n A Rebol program consists of three kinds of things: n Values, such Elements n A Rebol program consists of three kinds of things: n Values, such as numbers, booleans, etc. n n Blocks n n n Words represent things (such as variables) and actions (such as functions) Also comments--they begin with a semicolon ( ; ) Rebol is homoiconic: there is no distinction between “code” and “data, ” other than what the program does with it Rebol is interpreted, not compiled n n A block is a list of things enclosed in square brackets, [ ], and separated by white space Blocks are evaluated only when the program says to evaluate them Words n n (but there’s a lot of “etc. ”)

Values n n n Numbers: 5, 3. 14, 1. 2 E 7 Booleans: true, Values n n n Numbers: 5, 3. 14, 1. 2 E 7 Booleans: true, false Hex: #{FF 00 FF} Strings: "Hello world" Characters: #"a" Money: $4. 99, EUR$4. 99 URLs: http: //google. com Email: [email protected] com Files: %letter. txt Dates: 8 -Dec-2010 Times: 10: 30 AM tags: , n tags: , Tuples: 127. 0. 0. 1 Pairs: 1024 x 768 Issues: 1 -800 -555 -1212 Blocks: [red green blue] n There about 45 types n n n Conversion functions are to-type, e. g. to-char, tohex, to-image, etc.

Blocks n Blocks are things (values, words, other blocks) enclosed in square brackets n Blocks n Blocks are things (values, words, other blocks) enclosed in square brackets n n n There is no real distinction between code and data n n n [red green blue] [if 2 < 4 [print 4]] Thus, you can easily manipulate code Implication: Functions--or any chunks of code--are first class objects Blocks are evaluated if the program evaluates them, but not automatically

Words n There are five ways to use words: n As function names or Words n There are five ways to use words: n As function names or variables: fun, value n n n Setting a word to a value: 5, foo: func [i] [i < 10] Retrieving a value: : word n Useful for treating a function as a value, for example, to pass it as an argument to some other function Using a word as a symbol, standing for itself: 'Bob-Smith n Using the word as a refinement: dump/hex, now/date There are no reserved words n For example, you can redefine if n. . . but it’s probably not a good idea! n n When executed: Call the function or return the value

Evaluating a block n do will evaluate a block and return the last value Evaluating a block n do will evaluate a block and return the last value n n reduce will evaluate a block and return a block of results n n n >> do [now/date colors [colors $12] 4] == 4 >> data: reduce [now/date colors [colors $12] 4] == [12 -Dec-2013 [red green blue] [colors $12. 00] 4] Notice that only top-level elements are evaluated If a block contains both code and data, compose will evaluate only things within parentheses n >> compose [ now/date (now/date) ] == [now/date 12 -Dec-2013]

Control structures n n n n n do [. . . ] if expr Control structures n n n n n do [. . . ] if expr [. . . ] either expr [. . . ] while [expr] [. . . ] for i min max step [. . . ] until [. . . expr] loop count [. . . ] repeat var max [. . . ] forever [. . . ] switch/default var [ 1 [. . . ] 2 [. . . ] ] [. . . ]

Logic n n n n n false or none true or anything that isn’t Logic n n n n n false or none true or anything that isn’t false or none and x y or x y not x xor x y all [. . . ] any [. . . ] < <= = == != <> >= >

Numbers n n n +, -, *, /, **, // (remainder) exp value, log-10 Numbers n n n +, -, *, /, **, // (remainder) exp value, log-10 value, log-2 value, log-e value, square-root value absolute value, negate value min x y, max x y, sine, cosine, tangent, arcsine, arccosine, arctangent No precedence; evaluation is done left to right n Hence: >> 2 + 3 * 10 == 50

Useful Functions n n n read source returns the string read from the source Useful Functions n n n read source returns the string read from the source (file, url, etc. ) write dest data writes data to the destination (file, url, etc. ) ask question prompts the user with the question, returns the entered string input reads a line from the console to-integer value converts a value to an integer to-date value converts a value to a date to-file value converts a value to a filename prin data prints data without a line break print data prints data, appends a line break foreach act list [. . . ] executes the block for every element act in list now returns the current date/time

Defining functions n dump-i: [ print [ Defining functions n dump-i: [ print ["i =" i] ] n n n Not a function, just a block of code >> i: 7 == 7 >> dump-i: [ print ["i =" i] ] == [print ["i =" i]] >> do dump-i i = 7 dump-i: func [][ print ["i =" i] ] n n This is a “real” function >> dump-i: func [][ print ["i =" i] ] >> dump-i i = 7

Objects n n n account: make object! [ name: Objects n n n account: make object! [ name: "Flintstone" balance: $100 ss-number: #1234 -XX-4321 deposit: func [amount] [balance: balance + amount] withdraw: func [amount] [balance: balance - amount] ] print account/balance $100. 00 account/deposit $300 print ["Balance for" account/name "is" account/balance] Balance for Flintstone is $400. 00

Quick example: Text editor n view layout [ h 1 Quick example: Text editor n view layout [ h 1 "Text Editor: " f: field 600 "filename. txt" a: area 600 x 350 across btn "Load" [ f/text: request-file show f a/text: read to-file f/text show a ] btn "Save" [ write to-file request-file/save/file f/text alert "Saved" ] ]

Quick example: Email client n view layout[ h 1 Quick example: Email client n view layout[ h 1 "Send: " btn "Server settings" [ system/schemes/default/host: request-text/title "SMTP Server: " system/schemes/pop/host: request-text/title "POP Server: " system/schemes/default/user: request-text/title "SMTP User Name: " system/schemes/default/pass: request-text/title "SMTP Password: " system/user/email: to-email request-text/title "Your Email Addr: " ] a: field "[email protected] com" s: field "Subject" b: area btn "Send"[ send/subject to-email a/text b/text s/text alert "Sent" ] h 1 "Read: " f: field "pop: //user: [email protected] com" btn "Read" [editor read to-url f/text] ]

99 Bottles of Beer on the Wall n sing: func [count rest] [ prin 99 Bottles of Beer on the Wall n sing: func [count rest] [ prin pick ["99 bottles " "no bottles " "1 bottle "[count "bottles "]] min 4 count + 2 print rest ] for bottles 99 0 -1 [ sing bottles "of beer on the wall, " sing bottles "of beer. " print pick [ "Take one down, pass it around, " "Go to the store, buy some more, " ] bottles > 0 sing bottles - 1 "of beer on the wall. " print "" ]

References n n n Rebol Quick Start, http: //www. rebol. com/docs/quick-start. html, by Carl References n n n Rebol Quick Start, http: //www. rebol. com/docs/quick-start. html, by Carl Sassenrath, is a great introduction Learn REBOL, http: //www. re-bol. com/rebol. html, by Nick Antonaccio, is a very good, and very thorough, tutorial A Programming Tutorial for REBOL (PDF), http: //www. rebol. com/article/0201. html, by Victor Pavlu, is thorough, but possibly a bit out of date n n I got many examples from this tutorial The recommended book is REBOL—A Programmer’s Guide, by Olivier Auverlot and Peter William Alfred Wood

The End “Ideas that require people to reorganize their picture of the world provoke The End “Ideas that require people to reorganize their picture of the world provoke hostility. ” ― James Gleick, Chaos: The Making of a New Science