top | item 45629087

(no title)

Cosi1125 | 4 months ago

In R:

    > `if` = function(a, b) { print(a); print(b) }
    > if (2 < 5) { 6 }
    [1] TRUE
    [1] 6
(Rye also looks like a nice language, though!)

discuss

order

middayc|4 months ago

I used R for a while to do visualizations, but I never got to it's more homoiconic side, but I saw since then that it has it.

Can you also put code into { 6 }, like { print(6) }?

Does this overwrite R's if or how does the scoping in R work?

Cosi1125|4 months ago

The `6` is the code, I just put the first thing that came to my mind there :D In my example, if you replace it with a more sophisticated code block, R will evaluate it and print out the result of the evaluation.

Typically, you'd want to parse the unevaluated code, though:

    > `if` = function(a, b) { print(substitute(a)); print(substitute(b)) }
    > if (foo) { print("bar") }
    foo
    {
        print("bar")
    }
It overwrites `if`, in the current scope, of course.