Lamfunc

From Esolang
Jump to navigation Jump to search

Lamfunc is a functional esolang by User:PythonshellDebugwindow.

Memory

Lamfunc uses a mapping of variable names to values (e.g., a dictionary/associative array) stored in VARS. There is no limit on the amount of name-value pairs in VARS at any one time.

Syntax

Lamfunc programs are made up of a series of function calls and function definitions.

Function calls

To call a function, you write its name followed by any arguments, as this is a prefix language, so what would in Python look like f(g(x(), y()), h()); i() would look like this in Lamfunc:

f g x y h i

Function definitions

To define a function, you write F fname - code on its own line. (All function definitions are one line, and there are no nested function definitions.) To define a function called f that calls itself forever:

F f - f

A function c that takes two arguments and calls them both (either with b on its own or as an argument to a, depending on a's arg count):

F c a b - a b

Function redefinitions are invalid syntax.

Lambdas

If a function name fname is preceded by a period ., it will not be called, but instead it will return the function fname without immediately calling it. So this function is the identity function:

F id f - .f

Builtins

Lamfunc has eight builtin functions:

  • p takes one argument, prints it (prints its bits if given a number), and returns it.
  • eq takes two arguments, returns 1 if they are equal, else returns 0.
  • i takes three arguments; if the first argument is nonzero (see eq), it returns the second argument, otherwise it returns the third argument.
  • cb takes two numbers, combines their bits, and returns that (e.g., 0b10 and 0b110 would become 0b10110).
  • lb takes a number and returns its last bit.
  • fb takes a number and returns all but its last bit.
  • vs takes two arguments X and Y, sets VARS[X] to Y, and returns Y.
  • vg takes an argument X, and returns VARS[X] (or 0 for VARS[X] = undefined).

Edge cases

If there aren't enough arguments passed to a function inside a function definition, a lambda function will be returned that takes the rest of that function's required arguments and then calls it. As an example, if you have a function f that takes 3 arguments and prints them all, this function:

F example a b - f a b

and this call:

example .p .eq .i

would call example with .p and .eq, but example would need another argument due to f's argument count. So it would be called with .i, calling f with the three functions and therefore printing them.

Examples

While loop function

F _w c f - f while .c .f
F while c f - i c _w .c .f eq .i .eq

'Not' function (e.g. !x)

F not x - eq x eq .i .eq