What is the difference between "a: [b 1]" and "a: [b: 1]" in red?

What's the difference between

a: [b 1]
; and
a: [b: 1]

both give the same results for

> a/b
1

they are different for a/1. When do you use what? And the second is a set, what is the first?

+4
source share
2 answers

the second is a set, what is the first?

You can get the answers by looking at the type:

>> type? first [b 1]
== word!

>> type? first [b: 1]
== set-word!

What is the difference

When you use an expression a/b, you write something that acts like a SELECT statement, looking at "any type of word" matching bin the block indicated a, then returning the element after it to the block.

Red Rebol - "" SELECT, ""

>> (first [a:]) = (first [a]) ;-- default comparison
true

>> select [b 1] (quote b)
== 1

>> select [b: 1] (quote b)
== 1

, , /CASE ( " " ):

>> (first [a:]) == (first [a]) ;-- strict comparison
true

>> select/case [b: 1] (quote b)
== none

>> select/case [b: 1] (quote b:)
== 1

, -, , R3-Alpha, , 1% 0,01:

>> 1% = 0.01
== true ;-- both R3-Alpha and Red

>> select [0.01 "test"] 1%
== "test" ;-- in Red

>> select [0.01 "test"] 1%
== none ;-- in R3-Alpha

, .

?

.: -/Notation-wise , , , , , . SET-WORD! , , !. - , (, FUNCTION). , - , SET-WORD.

, . , , . , , ( x: :append/dup/only/10/a)

: PATH! Rebol , , , PICK, :

>> numbers: [3 2 1]

>> pick numbers 3
== 1 ;-- because the 3rd element is a 1

>> select numbers 3
== 2 ;-- because 2 comes after finding a 3

>> numbers/3
== 1 ;-- acts like PICK because (...)/3 uses an INTEGER!

... , , SELECT (), WORD!:

>> words: [a b c]

>> select words 'a
== b ;-- because b is the thing after a in the block

>> pick words 'a
;-- In Rebol this is an error, Red gives NONE at the moment

>> words/a
== b ;-- acts like SELECT because (...)/a uses a WORD!

, SELECT PICK , .

. - .

+9

- , ?

, [b 1], [b: 1] , . , : a word! set-word!, integer!.

a/b select a 'b, , 'b ( find). 'b :

red>> find [:b] 'b
== [:b]
red>> find [/b] 'b
== [/b]
red>> find ['b] 'b
== ['b]
red>> find [b] 'b
== [b]

, - , " ":

red>> 'b
== b

/case find select , , . , , , select/case.

, a/b, , b ( " " ):

red>> [b 1] = [b: 1]        ;-- loose comparison, used by `find` and `select`.
== true
red>> [b 1] == [b: 1]       ;-- strict comparison, used by `find/case` and `select/case`.
== false

a/1, .

. pick, a/1 pick a 1. , get-word! :

red>> c: 1
== 1
red>> a: [b 123]
== [b 1]
red>> a/:c
== b
red>> a: [b: 123]
== [b: 123]
red>> a/:c
== b:
red>> c: 2
== 2
red>> a/:c
== 123

Rebol Core: http://www.rebol.com/docs/core23/rebolcore-16.html#section-2.10

?

a/b vs a/1 , select pick.

[b 1] vs [b: 1] . , , set-word :

red>> a: [b:]
== [b:]
red>> append a 123
== [b: 123]
red>> c: object a
== make object! [
    b: 123
]

, set-word , "/", .

+2

Source: https://habr.com/ru/post/1658036/


All Articles