Foreword
Pravda is designed to best of both worlds advantage of 2 popular functional programming language.
So, you know they are Haskell and Lisp. Haskell is conservertive and mathematical functional programming language. On the other hand, Lisp has enthusiastic programmer who love that beatiful S-expression.
What do you think what happen if fusion Haskell and Lisp. It may created awesome language. So, the fusion is this, Pravda!
Setup
Installing is necessary process to programming Pravda. In this chapter, let it setup Pravda environment quickly.
First, clone the GitHub repository of Pravda interpreter to download.
git clone https://github.com/pravda-lang/pravda.git
And, move to its directory, enter below command to install.
cargo install --path .
If enter below command then show interactive console (the "REPL"), it was success!.
pravda
You may usually use the REPL to run Pravda program.
Instruction
Every instructions such even 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))
Look, Did you think that is near to Lisp style S-expression?
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.
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 lazy
or @
to the expression.
x = @{
+ 1 (input)
};
y = lazy{
+ 1 (input)
}
function eval
to evaluate it.
eval x
It's usually used in function if
to conditional branches.
Because, function if
needs block as arguments.
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;
}}}
})
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))
Partial application
If you provide too shortage argument to function, it returns partial applicated function.
add a b = + a b;
add 1
This code returns function that partial applicated by argument a
in number 1.
Lambda expression
It starts with lambda
or \
and surround by parentheses.
Inside parentheses is split by ->
, after is same to function.
lambda(n -> * n 2)
\(n -> * n 2)
Lambda expression is usually used in function map
etc in iterator.
map (range 10) lambda(n -> * n 2)