aboutsummaryrefslogtreecommitdiff
path: root/examples/hello.halk
blob: 5ff4a0b54e281caf7e21ecf19f6d3926fb0a01b9 (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
54
55
` comments in backticks `

` preprocessor directives `
#!PI# #3.14159#                                          ` define macros with #!<name># #<value># `
#IFNDEF# #HELLO#                                         ` predefined macros are: IF, AND, OR, NOT, ELIF, ELSE, FI, INCLUDE `
   #HELLO#                                               ` HELLO defined `
#ENDIF#

#INCLUDE# #io#                                           ` include the 'io' header `

` source code begins here `

: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;
}