aboutsummaryrefslogtreecommitdiff
path: root/examples/hello.halk
blob: 7ec14b8b91a335d693ee3c2e8de32814e5ee4a5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
` comments in backticks `

` preprocessor directives `
#MACRO##PI##3.14159#
#IFNDEF##HELLO#
   #DEF##HELLO#
#ENDIF#
` source code begins here `

str:hello = 'hello, ';                                   ` variables must be given a value at declaration `

void:greeting. str:to = {                                ` functions declared the same way as variables, but with a . after 
                                                         the name, followed by arguments `

   str:message = strcat.hello, to;                       ` function application is right-associative `

   stdo.message;                                         ` since 'io' was brought into global scope, we
                                                         do not prefix it with a namespace/ `
}

int:sum_all. int[]:_  = {                                ` variadic functions are possible with the reserved '_' argument,
                                                         which is treated as an array `
   return.foldl.sum, 0, _;  
}

namespace:new_namespace = {                              ` create a new namespace ` 
   int:value = 1;

   int:sum_all. int[]:_ = {
      return.foldl.sum,0,_;
   }
}

check_expect.(sum_all. 1, 2, 3, 4), 10;                  ` tests `
check_expect.
   (sum_all. 1, 2, 3, 4), 
   (new_namespace/sum_all. 1, 2, 3, 4);

int:fibonacci. int:n = {
   if.(eq.n, 0), {
      return.1;
   }, {
      return.sum.
         (fibonacci.(sub.n, 1)),
         (fibonacci.(sub.n, 2));
   }
}

int:main. str[]:args = {                                 ` where code will begin executing `
   greeting.'world.';                                    ` returns 'hello, world.' `

   return.0;
}