Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ask HN: Are there any languages where the function comes after the argument?
6 points by maest on July 22, 2019 | hide | past | favorite | 6 comments
So a language where function application looks like:

  arg function
That way, one can chain data transformations in a fairly natural way:

  my_data transformation1 transformation2
I'm not sure if this is a minor or major change to a language syntax and I'm curious if anyone's ever tried it.

I don't see any particularly compelling reasons why function application is done as

  foo(arg)
in virtually all languages. My guess is that this is inherited from conventions in mathematics.


Lisps are not in this camp by default, but in Clojure it's idiomatic to use the threading function to achieve a similar effect. For example,

  (inc (inc (inc 1)))
can be written

  (-> 1 inc inc inc)
(`inc` increments a number by one)

There are also several languages that allow method chaining. I.e.,

  thing.inc().inc().inc()


This is called Reverse Polish Notation. Forth uses it, as do a few other interesting languages.


Sure - they are called stack-based or concatenative languages (although they don't actually have to use stacks for implementation):

https://en.wikipedia.org/wiki/Stack-oriented_programming

The historically interesting/successful ones are mainly Forth and PostScript, but there are some successors of interest such as Joy.


Shell pipes are like this. Elixir took inspiration from it for its |> operator, although it works only for the first argument. You should check it out!


Factor works like that. It's dynamically typed and meta-programmable like Lisp and uses a global stack like Forth to pass data. By writing everything "backwards" (x) you get a syntax that is much more regular than what is possible in languages with Algol-like or even Lisp-like syntax. The language website is here https://factorcode.org/ It has a lot more code samples.

x - Of course, what is "backwards" and what is "forwards" is up to the reader. I'd argue that foo(arg) is backwards because we think of the expression as "arg processed by foo" Since arg comes before foo, it should be placed to the left of foo in the syntax.


Elixir has the pipe notation, which is somewhat similar




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: