When if is just a function
41–50 of 111 posts
Re: When if is just a function
#42This is interesting, but I'm not convinced it's better than the python it's being compared to. Memorizing and understanding the behavior of functions that perform control flow seems no easier than memorizing and understanding hardcoded syntax/keywords. The additional flexibility of making everything a first-class citizen allows people to write code that is too clever for its own good. I could be wrong but I think the…
> I could be wrong but I think there is a broad consensus that reflection is a Bad Idea. Reflection may be bad in practice for other reasons/conditions, but the lack of simple/minimal/regular primitive conventions in many languages, makes reflection a basket of baddies. The code blocks of Rye seem comparable to closures, which is a sensible thing to have. Once all code blocks are closures, there are fewer concepts to…
Re: When if is just a function
#43 data List a = Nil | Cons a (List a)
You can define its recursion principle by building a higher-order function that receives an element of your type and, for each constructor, receives a function that takes all the parameters of that constructor (with any recursive parameters replaced by `r`) and returns `r`.For `List` this becomes:
foldr :: (() -> r) -> (a -> r -> r) -> List a -> r
The eliminator for `Nil` can be simplified to `r` as `() -> r` is isomorphic to `r`: foldr :: r -> (a -> r -> r) -> List a -> r
foldr z f Nil = z
foldr z f (List a xs) = f a (foldr f z xs)
For `Bool`: data Bool = True | False
We get: bool :: a -> a -> Bool -> a
bool p q True = q
bool p q False = p
Which is precisely an If statement as a function!:D
Re: When if is just a function
#44Re: When if is just a function
#45Combinatory 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.
https://code.jsoftware.com/wiki/Essays/Tacit_Expressions
https://mlochbaum.github.io/BQN/doc/tacit.html and https://mlochbaum.github.io/BQN/doc/control.html
Forth, Factor and Uiua (which combines the above approach) don't use these concepts yet are also inherently point-free, and without lambdas so you definitely wouldn't be able to rely on functional techniques!
Re: When if is just a function
#46Re: When if is just a function
#47Earlier quoted context omitted.
BTW: In Lisps, if is still a special form / macro, because in Lisp lists are evaluated by default, in Rebols if can be a function because blocks (lists) aren't evaluated by default.
You can rarely successfully generalize about languages in the Lisp family. :) TXR Lisp: (relevant to this article) there is an iff function that takes functional arguments. Square the odd values in 0 to 9: 1> (mapcar [iff oddp square use] 0..10) (0 1 2 9 4 25 6 49 8 81) The use function is a synonym of identity : i.e. just use the incoming value as-is Square the even ones instead by inverting oddp with notf: 2> (mapc…
> TXR is an original notation for matching entire text documents or streams, inspired by the unification that underlies logic programming systems
This has me hooked.
Re: When if is just a function
#48Combinatory 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?…
Re: When if is just a function
#49This is interesting, but I'm not convinced it's better than the python it's being compared to. Memorizing and understanding the behavior of functions that perform control flow seems no easier than memorizing and understanding hardcoded syntax/keywords. The additional flexibility of making everything a first-class citizen allows people to write code that is too clever for its own good. I could be wrong but I think the…
E.g.
function funif (b, f) {
return (b && f())
}
If you want to do clever stuff. I never feel the need as I would rather abstract over bigger things.Re: When if is just a function
#50Useless unless the logical operators receive their rhs unevaluated. And that is generalized as a language feature.
In Kernel[1] for example, where operatives are an improved fexpr.
($define! $if
($vau (condition if-true if-false) env
($cond
((eval condition env) (eval if-true env))
(#t (eval if-false env)))))
$vau is similar to $lambda, except it doesn't implicitly evaluate its operands, and it implicitly receives it's caller's dynamic environment as a first class value which gets bound to env.$lambda is not actually a builtin in Kernel, but wrap is, which constructs an applicative by wrapping an operative.
($define! $lambda
($vau (args . body) env
(wrap (eval (list* $vau args #ignore body) env))))
All functions have an underlying operative which can be extracted with unwrap.