aboutsummaryrefslogtreecommitdiff
path: root/examples/hello.halk
blob: 62b83605aceaaa09ba8bc2d04961c7fc09dc60cc (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
:str:hello = 'hello, ';
: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 `

   io/stdo.message;                                      ` navigate namespaces with a '/' ` 
}

: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.';                                    ` prints 'hello, world.' `

   return.0;
}