Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Clojure devs use "xs" for function args?

In most languages there are conventions around arguments. Such as in nested loops you might use i at the top level, then j, then k.

But in Clojure I don't know what the convention is. I've seen, more often than not, the use of xs in function arguments; so why is that? Does it mean something specific? Are there other conventions?

like image 816
Integralist Avatar asked Feb 22 '15 17:02

Integralist


3 Answers

For the naming convention, you can refer to Clojure library coding standards

Specifically:

Follow clojure.core's example for idiomatic names like pred and coll.

in fns
    f, g, h - function input
    n - integer input usually a size
    index - integer index
    x, y - numbers
    s - string input
    coll - a collection
    pred - a predicate closure
    & more - variadic input
in macros
    expr - an expression
    body - a macro body
    binding - a macro binding vector
like image 124
Changgeng Avatar answered Oct 20 '22 02:10

Changgeng


I.m not sure it's a standard but very often the convention is x for a simple variable and xs for a variable that represents a sequence. The 's' stands for sequence or a plural, which is convenient.

For instance in destructuring you can use [x & xs] for the first and the following. You will find also idx for index and idxs for more indexes.

like image 24
T.Gounelle Avatar answered Oct 20 '22 02:10

T.Gounelle


Haskell and F# follow the same naming standard/pattern. The xs is the plural formed of x.

See also: https://stackoverflow.com/a/6267767/2370606

like image 3
Mike Harris Avatar answered Oct 20 '22 02:10

Mike Harris