Mailing List Archive: 49091 messages

## [REBOL] [bind example]

### From: rebol665::ifrance::com at: 26-Oct-2002 17:38

```
Hi rebollers,

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 "Sum      - 1 "
print "Product  - 2 "
print "Average  - 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 "Sum      - 1 "
print "Product  - 2 "
print "Average  - 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 "Sum      - 1 "
print "Product  - 2 "
print "Average  - 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 "Sum      - 1 "
print "Product  - 2 "
print "Average  - 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 "Sum      - 1 "
print "Product  - 2 "
print "Average  - 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
```