Live data from Hacker News

When if is just a function

ryelang.org

31–40 of 111 posts

Re: When if is just a function

#31
post #3

Combinatory programing offers functional control flow. (Here is a straight forward explanation: https://blog.zdsmith.com/series/combinatory-programming.html ) I was inspired to write `better-cond` in Janet: (defn better-cond [& pairs] (fn [& arg] (label result (defn argy [f] (if (> (length arg) 0) (f ;arg) (f arg))) # naming is hard (each [pred body] (partition 2 pairs) (when (argy pred) (return result (if (function?…

Do you have any recommendations for a language where you _have to_ use these concepts. I love playing with them but I find that unless i’m paying a lot of attention in most cases I fall back to a less functional style even in a language like Janet. I’d love to find a language where you largely have to use these combinatorial logic style functions so I can’t just default back to other styles.

Re: When if is just a function

#32
post #29
post #27

Earlier quoted context omitted.

Rye is foremost a REBOL and REBOL has this notion of many types of words at it's core: `word` - regular word, can evaluate to value it's bound to or call a funtion if bound to a function `word: "value"` - set-word, assignment that you noticed `probe :word` - get-word, always returns bound value, doesn't call a function if it's bound to a function, in Rye this is `?word`, because `:word` is left-set-word. `'word` - li…

> Rye also has left to right flow so it adds left-set-word. In Rye all assigned words with set-words are constants and they are used by default. So we also need a "mod-word", that is the double colon that you noticed, and left-mod-word So I would assume that the :i is actually constant within the loop body scope. That is, the loop function is doing something like this: ; i is not assigned in this scope evaluate {1 :i…

Yes, Rye follows REBOL in this case. Plain block invocation doesn't create it's own scope / context. That holds for do, if, either, loop, for, map, etc.

It would be costly to have this on by default. If you want separation there are many ways to achieve it. Rye has many functions related to contexts / scopes. For creating contexts in multiple ways and evaluating code inside contexts or with context as parent or isolated context, etc.

And a lot of builtins directly accept anonymous functions in place of blocks of code.

For example for loop also accepts function if you want separation and don't mind the cost.

    for { 1 2 3 } fn { x } { print x }
    ; which can also be written with fn1
    for { 1 2 3 } fn1 { .print }
    ; or latest experiment where we have syntax for 3 injected blocks 
    ; .() - same as "with"
    ; .[] - same as "reduce/with"
    ; .{} - same as "fn1"
    ; where it's already decided department from REBOL: 
    ; () is "do"
    ; [] is "vals"
    ; {} is literal block 
    for { 1 2 3 } .{ .print }

Re: When if is just a function

#33

I wish they showed the `else` syntax, because the traditional ALGOL-style if-then-else statement doesn't look native when shoved into most function call notations, unless you have something pretty interesting around named parameters and expressions delimiters.

[deleted]

Re: When if is just a function

#36
post #35

Interestingly, Excel provides "if" as a function: =if(condition, value-if-true, value-if-false)

Yes, Io language is closest to that syntax probably: https://iolanguage.org/guide/guide.html#Introduction

I wonder if it's more readable to non-programmers than control flow.

Re: When if is just a function

#38
post #37
post #15

Seems a bit like Tcl, which lets you create your own control structures like that.

Or redefine the language provided 'if' statement, in the case that one wanted to do so.

You can't really redefine if because everything is a constant, but you can define if in your own context yes.

Re: When if is just a function

#39
This discussion makes me so happy because people still care about programming languages and not just on stupid Java or whatever is making gobs of money. LISP should have a much larger following than it does, though I fully admit it has its own warts.

Re: When if is just a function

#40
post #24

Earlier quoted context omitted.

Certainly: I can tell the rye devs like the idea of everything being a function. But in their very first "language basics" section, they introduce assignment not as a function call, but as some kind of magic that happens when you have colons in a name. So when we get to the "looping" section, it is the first time we have seen a colon-having-name outside the context of assignment: > loop 3 { ::i , prns i } And it is e…

If you treat assignment as a function, then you have to reify environments as run-time objects, whereby you basically lose lexical scope. Lisp originally, as in LISP, had assignment as a function: it was called SET. To use it, you usually had to quote: (SET 'VAR 42). It worked without an environment parameter because variables were in a pervasive environment, but the quote was needed to get the variable symbol as a r…

So tcl handles it somewhat more elegantly, I think. It also has a set function, but does not require any special quoting because it uses the $ prefix to denote "get the value of this symbol":

    set a 5
    puts $a  #prints 5
and of course because it is modeled as a function (albeit an impure one) you can pass arguments to it as normal:

    set a b
    set $a 5  #equivalent to set b 5
    puts $b   #prints 5
of course, everything in tcl is a string, so this works too lol

    set a 5
    set $a b
    puts $5  #prints b
Post reply on HN