Skip to content

Document

梶塚太智 edited this page Sep 6, 2024 · 16 revisions

This is a documentation of Pravda programming language for beginner who can't understand even implementation.

Instruction

Every instructions such even adding operator, are function in Pravda.

It split by space. operator comes first, arguments comes after.

+ 1 2

If you want to nest, lower function calling should be surround by parentheses.

+ 1 2 (- 3 4 (* 5 6))

Define variable

Pravda variable is allowed to reassignment in spite of functional programming language.
Because it's to make Pravda more easy. I think variable binding that can see in usual functional is not user-friendly

It split by equal. the left is identify, the right is expression.

x = 5

This code defines variable "x" in number value 5.

Define function

It split by equal same to define variable case. the left is function header, split by space. first is function is identify, others are argument.

x2 n = * n 2

If you want to use mutable length argument, prefix ~ argument name. The passed mutable length argument will be list.

joinx ~list = join list "x"

Pattern matching

If you set value as function argument, it become pattern It only call when passed argument is matched.

fact 0 = 1;

If you set normal symbol as function argument. It call any case without configured pattern.

fact n = * n (fact (- n 1))

Lambda expression

It starts with lambda and surround by parentheses. Inside parentheses is split by ->, after is same to function.

lambda(n -> * n 2) 

Lambda expression is usually used in function map

map (range 10) lambda(n -> * n 2) 

Code block

If you surround by brace, the inside code will be block. The last expression in the block returns value.

{
  b = {
    c = 1;
    + c 1
  };
  + b 1
}

In this code, the block returns 3. variable "b" defined in the block is not allowed to access in outside.

Lazy evaluation

If you want to do lazy evaluation, prefix @ to the expression. function eval to evaluate it.

x = @{
    + 1 (input)
};
eval x

It's usually used in function if to conditional branches.

for (range 100) lambda(i -> {
    i = + i 1;
    if (equal 0 (% i 15)) @{
        print "FizzBuzz";
    } @{if (equal 0 (% i 3)) @{
        print "Fizz";
    } @{if (equal 0 (% i 5)) @{
        print "Buzz";
    } @{
       print i;
    }}}
})
Clone this wiki locally