a returns a

From Esolang
Jump to navigation Jump to search
The title of this article is incorrect because of technical limitations. The correct title is a{a}.

a{a} (pronounced a returns a) is an esolang that is based on recursion. A function is just a list of cases, and can be defined by itself or inside another function. Functions can also have other functions as inputs and return values. There are no built in ways to do most mathematical functions, and you must instead call a function that does that action. The only inbuilt data type is integers.

Syntax

Function declaration:

Syntax Description
name The first part is the name of the function, which can be any letter or _
[a,b,c...] the second part is a list of variable names. this can be any length, and include the same characters as function names
{a=b>c,d} the last part is a list of cases. a, b, and c can be any number or variable. the first case in this example checks to see if a and b are the same, and if the are, returns c. If a case is just a number or variable, then that is returned if no other cases are matched.

Example:

func[a]{a=1>2,a}

Functions can also be defined inside of another function. The declaration can use variables from the parent function, which are hardcoded into the new function.

A function is called by adding parenthesis to the end, which can also happen right after a function declaration. The number of inputs a function is supplied with must be equal to the number of inputs it was declared with. The program starts by calling the function named main, and if the main function is defined with inputs, then it asks for those inputs before running.

There are only 2 inbuilt functions, inc and dec. inc returns the input + 1. dec returns the input - 1.

Whitespace is ignored, so you can format your program however you want. You can also comment with # until end of line.

I/O

Input and output is handled with a function that acts like an array. The function is defined with an input starting at 0 which returns the first character of the string represented in ascii, then 1 which returns the second and so on. If the input is a string, then the function is called with such a function as input. Output is handled after the main function returns a value. If it's an integer then that integer is printed. If it's a function, it prints the ascii of that function called with 0, then called with a 1, and so on until it gets an end of file ascii value.

Code examples

Hello world:

str[i]{i=0>72, i=1>97, i=2>108, i=3>108, i=4>111, i=5>32, i=6>87, i=7>111, i=8>114, i=9>108, i=10>100, i=11>33,26}
main{str}

Cat:

main[i]{i}

Function wrapper:

a[f,g]{b[n]{f(g(n))}}

Replacing a case in a function:

caseReplacer[f,b,r]{g[a]{a=b>r,f(a)}} 

Useful functions:

choose[a,b,c]{c=-1>a,c=1>b} #chooses between a and b
sign[n]{n=0>0,sign2[n1,n2]{n1=0>-1,n2=0>1,sign2(inc(n1),dec(n2))}(n,n)} # gives the sign of a num
addSign[n,c]{c=0>n,c=1>inc(n),c=-1>dec(n)} #takes a num and a sign and returns the addition
invert[n]{n=0>0,n=1>-1,n=-1>1,a[n1,n2]{n1=0>n2,a(addSign(n1,invert(sign(n1))),addSign(n2,sign(n1)))}(n,0)} #inverts a number
add[a,b]{b=0>a,add(addSign(a,sign(b)),addSign(b,invert(sign(b))))} #addition
mulSign[a,b]{a=-1>invert(b),a=1>b;} #finds what the sign of a multiplication should be
posMul[a,b,total]{b=0>total;posMul(a,dec(b),add(a,total))} #finds the result of a multiplication between two positive numbers
mul[a,b]{choose(invert(posMul(a,b,0)),posMul(a,b,0),mulSign(sign(a),sign(b)))} #multiplication, including negatives
fib[n]{n=1>1,add(fib(dec(n)),fib(dec(dec(n))))} #returns nth term of the fibonacci sequence
factorial[n]{n=1>1,mul(n,factorial(dec(n)))} #returns n!