Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

repost [bind example]

 [1/2] from: rebol665::ifrance::com at: 2-Nov-2002 12:34


Hi rebollers, This is a repost as I have had no reply to my first post which occurs during the "HOF war between Ladislav, Jan and Gabrielle". As I intended to put this on my site somedays, I just want to be sure that there is no big *mistake* --- Here is an example of bind usage I think valuable for newbies. I fall into this while transforming a "pascal-like" program to produce a more rebolistic code. It is a bit long and fairly simplistic but each step can be tested. Hope some of you will learn a bit from it. ---The starting point That's what I call a "pascal-like" program. Rebol [title: "Exemple 10.1"] nb1: to-decimal ask "Enter number 1 :" nb2: to-decimal ask "Enter number 2 :" nb3: to-decimal ask "Enter number 3 :" print "Your choice" print "Sum - 1 " print "Product - 2 " print "Average - 3 " choice: ask "1|2|3 ? " switch choice [ "1" [print ["Sum is " nb1 + nb2 + nb3] ] "2" [print ["Product is " nb1 * nb2 * nb3]] "3" [print ["Average is " (nb1 + nb2 + nb3) / 3]] ] ---Code is data In Rebol, code is data, data is code. So this version uses block of data to store the computation code. Rebol [title: "Exemple 10.2"] ; prepared block b-sum: [nb1 + nb2 + nb3] b-prod: [nb1 * nb2 * nb3] b-aver: [(nb1 + nb2 + nb3) / 3] nb1: to-decimal ask "Enter number 1 :" nb2: to-decimal ask "Enter number 2 :" nb3: to-decimal ask "Enter number 3 :" print "Your choice" print "Sum - 1 " print "Product - 2 " print "Average - 3 " choice: ask "1|2|3 ? " switch choice [ "1" [print ["Sum is " do b-sum]] "2" [print ["Product is " do b-prod]] "3" [print ["Average is " do b-aver]] ] Rebol [title: "Exemple 10.3"] ---Now with a function Doing the code is now performed in a function: fn-calculate. ; prepared block b-sum: [nb1 + nb2 + nb3] b-prod: [nb1 * nb2 * nb3] b-aver: [(nb1 + nb2 + nb3) / 3] fn-calculate: func [cb [block!]][ return do cb ] nb1: to-decimal ask "Enter number 1 :" nb2: to-decimal ask "Enter number 2 :" nb3: to-decimal ask "Enter number 3 :" print "Your choice" print "Sum - 1 " print "Product - 2 " print "Average - 3 " choice: ask "1|2|3 ? " switch choice [ "1" [calcul-bloc: b-sum] "2" [calcul-bloc: b-prod] "3" [calcul-bloc: b-aver] ] print ["Result: " fn-calculate calcul-bloc] ---The failure Beware this version is incorrect ! The goal here was to send to the function the values as parameters and the code block. Note that num1, num2, num3 are used in the main part. This is important because now nb1 do not exist anymore in the global context. So the function gives an error. ** Script Error: nb1 has no value ** Where: fn-calculate ** Near: nb1 * nb2 * nb3 Rebol [title: "Exemple 10.4"] ; prepared block b-sum: [nb1 + nb2 + nb3] b-prod: [nb1 * nb2 * nb3] b-aver: [(nb1 + nb2 + nb3) / 3] fn-calculate: func [ nb1 [decimal!] nb2 [decimal!] nb3 [decimal!] cb [block!] ][ return do cb ] num1: to-decimal ask "Enter number 1 :" num2: to-decimal ask "Enter number 2 :" num3: to-decimal ask "Enter number 3 :" print "Your choice" print "Sum - 1 " print "Product - 2 " print "Average - 3 " choice: ask "1|2|3 ? " switch choice [ "1" [calcul-bloc: b-sum] "2" [calcul-bloc: b-prod] "3" [calcul-bloc: b-aver] ] print ["Result: " fn-calculate num1 num2 num3 calcul-bloc] ---The solution The solution is to bind the block with the local word 'nb1. Rebol [title: "Exemple 10.4"] ; prepared block b-sum: [nb1 + nb2 + nb3] b-prod: [nb1 * nb2 * nb3] b-aver: [(nb1 + nb2 + nb3) / 3] fn-calculate: func [ nb1 [decimal!] nb2 [decimal!] nb3 [decimal!] cb [block!] ][ return do bind cb 'nb1 ] num1: to-decimal ask "Enter number 1 :" num2: to-decimal ask "Enter number 2 :" num3: to-decimal ask "Enter number 3 :" print "Your choice" print "Sum - 1 " print "Product - 2 " print "Average - 3 " choice: ask "1|2|3 ? " switch choice [ "1" [calcul-bloc: b-sum] "2" [calcul-bloc: b-prod] "3" [calcul-bloc: b-aver] ] print ["Result: " fn-calculate num1 num2 num3 calcul-bloc] ---Conclusion Bind is needed to attach values to words. Values of word are stored in a context. Here for example, num1, num2, num3 are in the global context, while nb1, nb2, nb3 lives in the fn-calculate context. Because nb1, nb2, nb3 are on the same context, all the following binding are equivalent: bind cb 'nb1 bind cb 'nb2 bind cb 'nb3 Each of these lines reads : the values for the words in block cb are to be found in the context of the word (nb1 | nb2 |nb3). Ciao Pat __________________________________________________________________ Haut Débit: Modem offert soit 150,92 euros remboursés sur le Pack eXtense de Wanadoo ! Profitez du Haut Débit à partir de 30 euros/mois : http://www.ifrance.com/_reloc/w

 [2/2] from: lmecir:mbox:vol:cz at: 2-Nov-2002 21:15


Hi Pat, it is good to have simple and correct examples of BIND usage. Your example surely *is* correct. It has got only one disadvantage: it is fairly simple to do that without BIND, if we e.g. use the way Gabriele promotes: fn-calculate: function [ nb1 [decimal!] nb2 [decimal!] nb3 [decimal!] cb [block!] ] [f] [ f: func [nb1 nb2 nb3] cb f nb1 nb2 nb3 ] Your example surely has a didactical value and you can use it. Nevertheless, it may be useful to show the readers, that this "workaround" is possible as well. Moreover, Gabriele's "functional" approach looks safer sometimes (and more natural, at least for some users). When you posted your example, I tried to suggest a simple example, where BIND would be better, than a "functional" solution. Initially I considered my CFOR or SFUN implementation, but, unfortunately, I found out, that they didn't use BIND at all! Best regards -L