-
Notifications
You must be signed in to change notification settings - Fork 13
2.1 Taskell: the Haskell implementation in Tamgu (en)
Tamgu is a real functional language. Unlike many languages that integrate functional features, Tamgu offers a real functional formalism that works in parallel with the rest of the code.
You can write your functions, data structures and call them from your Tamgu program.
Taskell is a relatively faithful transformation of Haskell to adapt it to Tamgu.
First of all, a Taskell expression is always enclosed with <...>. You can easily and usefully use these expressions to write lambdas.
For example, Taskell's "map" operator applies an operation to a vector.
vector v = <map (+1)[1..10]>;
The line above will stow in v the values: [2,3,4,5,6,7,8,9,10,11].
Most of Haskell's operators have been integrated: filter, take, takeWhile, takeWhile, fold etc.
You can also iterate on an external vector to perform your own operations:
vector v =[1..10];
vector vv = < x*2 | x <- v>;
Your expression can also be used to filter out some values:
vector v =[1..10];
vector vv = < x*2 | x <- v, odd(x)>;
In the above example, we only keep and compute odd values.
A function is declared as follows:
<addstr(x,y) = x+y>
To type the parameters, you must declare before declaring your function, a structure that describes what the function receives and what it returns:
<addstr:: string -> string -> string -> string>
<addstr(x,y) = x+y>
In the description above, we indicate that the function takes two strings as input and returns a string.
Let's take an example:
We will implement the function: joined which transforms a vector into a string.
<joined:: vector -> string>
<joined(v) = x | x <- v>
Taskell also accepts data structure definition together with their derivations and their methods. Below is an example of a piece of code that computes the right surface for the right object:
// -- We first create a Shape object with two derivations: Circle and Rectangle
// -- They can display their content hence "Show"
<data Shape = Circle float float float |
Rectangle float float float float
deriving (Show)>
// -- A Surface takes a Shape as input and returns a float
<Surface :: Shape -> float>
<Surface(Circle _ _ r) = 2π×r²> // -- only the radius is useful
<Surface(Rectangle x y xx yy) = abs(xx-x) × abs(yy-y)>
float c=Surface(<Circle 10 20 30>); // -- we compute the surface for a circle
float r=Surface(<Rectangle 20 20 40 40>); // -- we compute the surface for a rectangle