code equivalences
This table showcases how to do various common tasks in a few different programming languages. Similar to Rosetta Code, but may work better as a quick reference.
The currently covered languages are: Common Lisp, Guile, Emacs Lisp, Pharo, Factor, Raku, Python, Tcl, SuperCollider, Lua, Fennel, Bash, Fish, JavaScript, and Rust.
In descending order of coverage, the languages are: Common Lisp (105/105), Guile (102/105), Emacs Lisp (100/105), Factor (94/105), SuperCollider (93/105), Pharo (86/105), Raku (83/105), Fish (79/105), Python (71/105), Bash (69/105), Fennel (65/105), Lua (59/105), Tcl (53/105), JavaScript (52/105), Rust (36/105).
The following external pages may also be useful references for these languages:
- Common Lisp Cheat Sheet
- Common Lisp vs. Scheme
- Simply Scheme: Appendix B: Common Lisp
- Elisp Reference Sheet
- Elisp Cheatsheet for Python Programmers
- Pharo syntax in a nutshell
- Not So Terse Guide to Pharo
- Raku syntax
- Learn Raku in Y Minutes
- Learning Raku from Python, in a nutshell
- Raku Guide
- Tcl/Tk tutorial
- Pure Bash Bible
Contents
- Fundamental Types
- Syntax
- Functions
- Basics
- Math
- Sequences
- Strings
- Hashtables
- Input / Output
- Namespaces / Modules
- Metaprogramming
- Introspection
- Info
Table
Show columns:
| task | Common Lisp | Guile | Emacs Lisp | Pharo | Factor | Raku | Python | Tcl | SuperCollider | Lua | Fennel | Bash | Fish | JavaScript | Rust |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Fundamental Types | |||||||||||||||
| Null | When differentiation between false and null is needed, some libraries use the symbol naming the type of the nil value ( null). | | | | | | | unsupported | | | | unsupported | unsupported | | unsupported Rust does not have a null value; the Option type is usually used for handling null or absent values. |
| Booleans | | | | | | | | | | | | | | | |
| Special atoms | | | (?) | ||||||||||||
| Special variables | | | (?) | | | | (?) | (?) | | (?) | |||||
| Symbol | | | | | Symbols defined with SYMBOL: are subsequently referred to using barewords. | | unsupported | unsupported | | unsupported | Fennel supports symbols, however they evaluate to their name (i.e. strings). | unsupported | unsupported | | (?) |
| String | | | | | | | | Curly braces ( { and }) are used for literal strings (no interpolation). | | | | | | | |
| Character | | | Emacs represents characters with Unicode codepoints, so this code results in 70. | | Factor represents characters with Unicode codepoints, so this code results in 70. | unsupported | unsupported | unsupported | | unsupported | unsupported | unsupported | unsupported | unsupported | |
| Unicode character (by codepoint) | | | Emacs represents characters with Unicode codepoints. A list of codepoints can be converted to a string with concat. | | Factor represents characters with their Unicode codepoints. An array of codepoints can be converted to a string with >string. | Raku does not have a character type; this example uses interpolation to produce a single-character string. | Python does not have a character type; chr returns a single-character string. | Tcl does not have a character type; this example uses the Unicode character escape sequence to create a single-character string from the codepoint at hex 0x2764 (dec 10084). | SuperCollider only supports ASCII, not Unicode. | unsupported | unsupported | unsupported | unsupported | JavaScript does not have a character type; this example uses the Unicode character escape sequence to create a single-character string from the codepoint at hex 0x2764 (dec 10084). | (?) |
| Unicode character (by name) | | | Emacs represents characters as Unicode codepoints, so this code results in 10084. | This example requires the Unicode-Character-DataUnicode-Character-Data external library. Unicode characters can be looked up by their code point in stock Pharo using code like Character codePoint: 10084. | Factor represents characters as Unicode codepoints, so this code results in 10084. | Raku does not have a character type; this example uses interpolation to produce a single-character string. | Python does not have a character type; this example uses interpolation to produce a single-character string. | (?) | unsupported | unsupported | unsupported | unsupported | unsupported | unsupported | (?) |
| Fraction / Ratio | | | unsupported | | | Raku natively supports fractions, but prints them as floats by default. The raku method can be used to confirm their representation as readable Raku code. | | unsupported | | unsupported | unsupported | unsupported | unsupported | unsupported | (?) |
| Sequence (idiomatic) | | | | | | | | | | | | | | | (?) |
| Linked list | | | | Pharo supports linked lists, but arrays are more idiomatic. | Factor supports linked lists, but arrays are more idiomatic. | | | unsupported | | unsupported | unsupported | unsupported | unsupported | unsupported | (?) |
| Array | | | | | | | Python refers to its arrays as lists, however they are not traditional linked lists a la Lisp. | | | Lua uses tables as both arrays and as hashtables. | Fennel uses tables as both arrays and hashtables, albeit with different syntax | | | | (?) |
| Syntax | |||||||||||||||
| Comment | | | | | | | | | | | | | | | |
| Multi-line comment | | | unsupported | | Factor supports a syntax that resembles multi-line comments, but single line comments (with !) are much more common. | Note that ( and ) can be replaced by any delimiter pair, such as [] or {}. | | unsupported | | | unsupported | unsupported | unsupported | | |
| Basic math | | | | | | | | | | | | | | | |
| Define variable | | | | | | | | | | | | | | | |
| Define variable with initial value | | | | | | | | | | | | | | | |
| Define variable with initial value, not overwriting existing value | | | | unsupported Pharo does not allow referencing undefined variables, and all defined variables default to nil. | | unsupported Raku has the //= operator, which sets a variable's value only if it is not already set, but this doesn't seem to work when combined with variable declarations such as my or our. | (?) | | Variables default to nil, and the ? operator returns the first non-nil value. | (?) | (?) | (?) | | (?) | (?) |
| Functions | |||||||||||||||
| Anonymous function (lambda) | | | | | | | | (?) | | | | unsupported | unsupported | (?) | |
| Define function | | | | Typically, functions are defined as methods in a class rather than as blocks (anonymous functions) as shown here. | | | | | | | | | | (?) | |
| Define variadic function | | | | unsupported | unsupported Stack languages like Factor do not support variadic functions in the traditional sense. Alternatives include passing a sequence, or writing a parsing word with SYNTAX:. | | | | | | | | | | (?) |
| Call function with arguments | | | | | | | | | | | | | | | |
| Call function specified in variable | | | | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) |
| Call function with arguments specified as sequence | | | | (?) | (?) | (?) | (?) | (?) | (?) | (?) | | (?) | (?) | (?) | (?) |
| Basics | |||||||||||||||
| Print (no newline) | | | | | | | | | | | | | | (?) | |
| Print (with newline) | | | | | | | | | | | | | | (?) | |
| If statement | | | | | | | | | | | | | | (?) | |
| Get type of datum | | | | | | | | In Tcl 8.7 and above, ::tcl::datatype $datum can be used. | | | | unsupported | unsupported | | (?) |
| Get object's string representation | | | | | | | | (?) | | Lua does not have a built-in way to get a string representation of a table. | | unsupported | unsupported | | |
| Print object's string representation | | | | | | | | (?) | | Lua's print will not print the contents of tables or other compound objects; this functionality has to be implemented by the user. For tables, it can be done with for key, value in pairs(obj) do print(key, value) end | | | | | |
| Read a line of text | | | | | | | | | SuperCollider's EZText is a graphical text field widget. Its callback action must be set to a function which binds the field's text to a variable (f in this case). | | | bash's read should be called with a variable name to specify where to write the text into. Without it, text is read, but discarded. | | | (?) |
| Math | |||||||||||||||
| Infinity | Not portable across implementations - only works in SBCL. | | unsupported | | | | (?) | | | (?) | (?) | (?) | (?) | (?) | (?) |
| NaN | | | | | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) |
| Pi | | | | | | | (?) | (?) | (?) | (?) | (?) | (?) | | (?) | (?) |
| Random integer from 0 below N | | | | | | | | | | | | | | | (?) |
| Random float from 0 below N | n must be a float. | n must be a float. | n must be a float. | | n must be a float. | | | | n must be a float. | (?) | (?) | (?) | (?) | | (?) |
| Sequences | |||||||||||||||
| Sequence length | | | | | | | | | | | | | | | |
| First sequence element | | | | | | | | | | | | | | | |
| Second sequence element | | | | | | | | | | | | | | | |
| Last sequence element | | | | | | | | (?) | | | | | | | (?) |
| Sequence element at index N | | | | | | | | | | Note that Lua tables start from index 1. | Since Fennel compiles to Lua, its tables also start from index 1 | | Note that Fish lists start from index 1. | | |
| First N elements of sequence | | | | | | | | (?) | | | | | | (?) | (?) |
| Sequence without first N elements | | | | | | | | (?) | | | | (?) | | (?) | (?) |
| Sequence of last N elements | | | | | | | | (?) | | | | (?) | | (?) | (?) |
| Sequence without last N elements | | | | | | | | (?) | | | | (?) | | (?) | (?) |
| Sequence subsequence | | This only works for lists, not other types of sequences. | | | | | | (?) | | | | (?) | | (?) | (?) |
| Filter sequence to items true of predicate | | | | | | | | (?) | | (?) | (?) | (?) | (?) | (?) | (?) |
| Filter sequence to items false of predicate | | | | | | (?) | | (?) | | (?) | (?) | (?) | (?) | (?) | (?) |
| Remove instances of item from sequence | | | | (?) | | (?) | | (?) | | (?) | (?) | (?) | (?) | (?) | (?) |
| Test for item in sequence | | | | | | | | (?) | | (?) | (?) | (?) | | (?) | (?) |
| Get index of item in sequence | | | | | | (?) | | (?) | | (?) | (?) | (?) | | (?) | (?) |
| Get first item in sequence matching predicate | | | | | find returns both the matching item and its index; nip can be used to discard the index. | | (?) | (?) | | (?) | (?) | (?) | (?) | (?) | (?) |
| Get index of first element matching predicate | | | | (?) | find returns both the matching item and its index; drop can be used to discard the item. | | (?) | (?) | | (?) | (?) | (?) | (?) | (?) | (?) |
| Test if all items in sequence match predicate | | | | (?) | | (?) | (?) | (?) | | (?) | (?) | (?) | (?) | (?) | (?) |
| Count occurrences of item in sequence | | | | | | (?) | | (?) | | (?) | (?) | (?) | (?) | (?) | (?) |
| Count items in sequence matching predicate | | | | | | (?) | | (?) | | (?) | (?) | (?) | (?) | (?) | (?) |
| Apply function to each element, collecting results (map) | | | | | | | (?) | (?) | | (?) | (?) | (?) | (?) | (?) | (?) |
| Combine elements of a sequence with a function (fold/reduce) | | | | | | | (?) | (?) | | (?) | (?) | (?) | (?) | (?) | (?) |
| Elements common to two sequences (set intersection) | | | | (?) | | (?) | (?) | (?) | | (?) | (?) | (?) | (?) | (?) | (?) |
| Elements only in the first of two sequences (set difference) | | | | | | (?) | (?) | (?) | | (?) | (?) | (?) | (?) | (?) | (?) |
| Combine two sequences without duplicates (set union) | | | | | | (?) | (?) | (?) | | (?) | (?) | (?) | (?) | (?) | (?) |
| Remove duplicates in sequence | | | | (?) | | (?) | (?) | (?) | Converting to a set will not preserve the order of elements in the array. | (?) | (?) | (?) | (?) | (?) | (?) |
| Remove duplicates in sequence, comparing with predicate | | | | (?) | There may be a better way of doing this than is implemented here. Typically, deduplication with the standard comparison check can be done more simply with members. | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) |
| Reverse sequence | Common Lisp also has an nreverse function which reverses in place. | | | | | | Python's list.reverse method reverses in place. | (?) | | (?) | (?) | (?) | (?) | JavaScript also has a .reverse() method which reverses in place. | (?) |
| Sort sequence in natural order | | | | | | (?) | (?) | (?) | | (?) | (?) | (?) | (?) | | (?) |
| Sort sequence by predicate | | | | | | (?) | (?) | (?) | | (?) | (?) | (?) | (?) | | (?) |
| Get random element of sequence | This uses a function from the essential and ubiquitous utility library Alexandria. | | (?) | (?) | | | (?) | (?) | (?) | (?) | (?) | (?) | | (?) | (?) |
| Strings | |||||||||||||||
| Unicode support | implementation-dependent Most popular Common Lisp implementations fully support Unicode: SBCL, CCL, LispWorks, Allegro, ECL, and ABCL. Older and defunct implementations such as CLISP, CMUCL, and GCL do not. | supported | supported | supported | supported | supported | supported | supported Supported as of v8.1. | unsupported Unicode can be put in strings, however characters can only be 1 byte long. | unsupported | unsupported | supported | supported | supported | supported |
| Unicode string length | | | | | | | | | | | | | | | |
| String concatenation | | | | | | | | (?) | | (?) | | | | (?) | (?) |
| String subsequence | | | | | | | | (?) | | (?) | | (?) | | (?) | (?) |
| Lowercase string | | | | | | | | | | | | | | | (?) |
| Hashtables | |||||||||||||||
| Hash table | | | | | <hashtable> requires an initial size. | | (?) | | | (?) | Like Lua, Fennel uses tables for arrays and "hash tables". | bash requires a name for the variable when declaring it a hash. | unsupported | (?) | (?) |
| Hash table literal | unsupported | unsupported | | unsupported | | | (?) | (?) | SuperCollider's events (used for this example) do not support looking up keys by strings, so symbols are used. | | | | unsupported | (?) | (?) |
| Get the value of key in hash table | | | | | | | (?) | | | (?) | | | unsupported | (?) | (?) |
| Set the value of key in hash table | | | | | | | (?) | | | (?) | | | unsupported | (?) | (?) |
| Get list of keys in hash table | | | | | | | (?) | | | (?) | (?) | | unsupported | (?) | (?) |
| Input / Output | |||||||||||||||
| Namespaces / Modules | |||||||||||||||
| Define a namespace | | | unsupported Emacs does not support namespaces. Instead, the convention is to prefix all of a package's symbols with the name of the package and a dash; for example cl-position is the position function from the cl-lib library. | (?) | Typically new vocabularies (namespaces) are defined with IN:, which also sets the current active vocabulary, simply switching to it if it already exists. | | (?) | (?) | | (?) | (?) | unsupported | unsupported | (?) | (?) |
| Get the current namespace | | | unsupported | (?) | | (?) | (?) | (?) | | (?) | (?) | unsupported | unsupported | (?) | (?) |
| Set the current namespace | | | unsupported | (?) | IN: will also create the vocabulary if it doesn't already exist. | (?) | (?) | (?) | | (?) | (?) | unsupported | unsupported | (?) | (?) |
| Use a namespace | | | unsupported | (?) | | | (?) | (?) | In this example, env is the environment (namespace) being used, and func is the function being evaluated in that environment. | (?) | (?) | unsupported | unsupported | (?) | (?) |
| Reference something inside a namespace | A single colon is for exported symbols; to reference un-exported symbols, use ::, i.e. my-module::my-thing. | caveat In Guile, a module's prefix is set by the user of the module, using the #:prefix keyword of use-modules and similar macros. | unsupported | (?) | (?) | | (?) | (?) | | (?) | (?) | unsupported | unsupported | (?) | (?) |
| Metaprogramming | |||||||||||||||
| Define macro | | | | (?) | (?) | | unsupported | (?) | unsupported | unsupported | | unsupported | unsupported | unsupported | (?) |
| Introspection | |||||||||||||||
| Documentation | object-type must be the type of the object you want documentation for, since Common Lisp is a Lisp-2. Supported documentation types are function, variable, method-combination, compiler-macro, setf, structure, and type. | | | (?) | (?) | | (?) | (?) | (?) | (?) | (?) | unsupported | unsupported | (?) | (?) |
| Get visible symbols | apropos prints the symbols, returning no values; to get the results as a list, use apropos-list instead. | | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) |
| Search visible symbols | | | | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) |
| Describe object | | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | (?) | unsupported | unsupported | (?) | (?) |
| Inspect object | Most Lisp environments also have shortcuts to inspect in their own inspectors. | ,inspect is a REPL command; it cannot be used inside standard Guile code. | unsupported Emacs does not include an inspector in a traditional sense, however a third-party one is available. | Pharo objects can also be inspected in its GUI using the Ctrl+g shortcut. | (?) | (?) | (?) | (?) | (?) | (?) | (?) | unsupported | unsupported | (?) | (?) |
| Get object's instance variables | | (?) | (?) | | | | | (?) | (?) | (?) | (?) | unsupported | unsupported | (?) | (?) |
| Get object's methods | This function only returns direct methods of the specified class; it does not return all functions applicable to the object. | (?) | (?) | | (?) | | | (?) | | (?) | (?) | unsupported | unsupported | (?) | (?) |
| Info | |||||||||||||||
| Software |
|
|
|
|
|
|
|
|
|
| (?) | ||||