Functional

From Esolang
Jump to: navigation, search

Functional is an esoteric programming language that only use function calls like this:

print("Hello world!")

Even control structures, loops and variable declarations are function calls:

print("What's your name?\n")
set(name, input())
printf("Hello, %s\n", name)

Even comments are function calls:

rem("This is a comment.")

or

nop("nop function does nothing")

Data types

There are three data types:

  • Barewords: Unquoted string or numbers, like identifiers in other programming languages, for example: set(bareword, "string value") Most functions will treat barewords as an identifier. Numbers such as 12 are also barewords.
  • Strings: They can be quoted with "" or ''. For example: "Hello, world!", 'Naruto\'s file'
  • Code blocks: They are enclosed with { and }. For example: if (equals(a,b), {print("a equals to b") } )

Standard functions

  • set(var, value) Set variable to value
  • if(condition, code) Execute the code if condition is true or not zero/null.
  • if_else(condition, code, else) Expands to if(condition){code}else{else}
  • loop(code) An infinite loop.
  • while(condition, code) A while loop.
  • for(init, condition, incr, code)
  • function(name, ..., code) Create a new function. The ... lists all arguments.

Cat program

loop(
  {
    set(user_input, input())
    print(concat(user_input, "\n"))
  }
)

Functional as a Java scripting language

If the Functional virtual machine is written in Java, it can let the Functional programs call Java methods and create Java objects, like what the Rhino JavaScript engine did.

try_catch(
  {
    set(fs, new(java.io.FileWriter, "out.txt"))
    set(w, new(java.io.BufferedWriter, fs))
    w.write("Hello, world!")
    w.close()
  }, java.lang.Exception, e {
    System.err.println("Error: ")
    System.err.println(e.toString())
  })

See also