Skip to content

Getting Started with EK

Emil Pedersen edited this page Oct 4, 2024 · 2 revisions

Getting started with the EK programming language

The EK programming language is a simple functional language. It is designed to be easy to learn and use, while still being powerful enough to write useful programs.

The important difference between EK and other languages is that EK is a functional language, where functions can be prefix, infix, or postfix. This means that you can write 1 + 2 or + 1 2 or 1 2 +, depending on how the function + is defined.

EK is impure, meaning that it allows side effects. This means that you can write programs that interact with the outside world, like printing to the screen or reading from the keyboard.

EK is dynamically typed, meaning that types are checked at runtime. Type annotations are mostly optional, and are ignored by the compiler.

Installation & Usage

Make sure you have EK installed. You can download the latest version from the releases page, and put it in your PATH. Alternatively, you can build it from source, and install it using make install.

To compile a program, you can run ekc <filename>. This will compile the program and output the result to a.out.

You can then run the program using ./a.out, or using ek a.out.

Alternatively, you can run ekc -t result <filename> to compile and run the program in one step.

Hello, world!

To make a program that prints "Hello, world!", you can write:

import std
fn main = print "Hello, world!"

This defines a function called main that takes no arguments and prints "Hello, world!" to the screen. The fn keyword is used to define functions, and the = is used to assign a value to it. The print _ function takes a string as an argument and prints it to the screen.

All EK programs must have a function called main defined, and it must take no arguments. This is the entry point of the program.

Note that the import std line is required to use the print function. The std module contains many useful functions, and must be imported to use them. You can alternatively import another module, or define your own functions.

Variables

There are no variables in EK, as it is a functional language. Instead, you can define functions that take no argument to create constants. For example, to define a constant called x that is equal to 1, you can write:

fn x = 1

This defines a function called x that takes no arguments and returns 1. You can then use x in other functions, like this:

fn main = print x

This will print 1 to the screen.

Functions

Functions are defined using the fn keyword. For example, to define a prefix function add that takes two arguments and returns their sum, you can write:

fn add (a) (b) = a + b

This defines a function called add that takes two arguments, a and b, and returns their sum. The + infix function is defined in the std module, and takes two numbers as arguments and returns their sum.

You can also define infix functions, like this:

fn (a) add (b) = a + b

Or, you can even define it as a postfix function, if you like RPN:

fn (a) (b) add = a + b

More information about functions can be found in the Functions section.

Modules

Modules are used to organize functions into groups. For example, the std module contains many useful functions, like print and +. To use a function from a module, you must import it.

A module is just a file with the .ek extension. For instance, the std module is defined in the file std.ek.

Comments

Like most languages, EK supports comments. There are two types of comments: single-line comments and multi-line comments.

Single-line comments start with // and continue until the end of the line.

Multi-line comments start with /* and end with */. They can span multiple lines.

Types

There are several kinds of types in EK:

  • Atoms: These are used to represent a value with no data. For example, true, false and void are atoms.
  • Integers: Unlike other languages, integers have no minimum or maximum value.
  • Primitves: Strings, Floats
  • Structs: These are values that contain multiple elements.

You can declare your own atoms and your own structures. You can also make type aliases for typing your code. For more information, see the Types in EK page.

Clone this wiki locally