cmc/cleberg.net

My personal web garden & blog.

clone: git clone https://gitbay.org/cmc/cleberg.net.git

main: content/blog/2023-02-02-exploring-hare.org · raw

  1#+date:        [2023-02-02 Thu 00:00:00]
  2#+title:       Hare Lang: Notes from First Contact
  3#+description: First impressions of the Hare programming language.
  4#+slug:        exploring-hare
  5#+filetags:    :linux:
  6
  7* A Quick Note
  8
  9By no means am I a professional developer, so this post will be rather
 10short. I won't be going into depth on the specification or anything that
 11technical.
 12
 13Instead, I will simply be talking about how I (a relatively basic
 14hobbyist programmer) have been playing with Hare and what intrigues me
 15about the language.
 16
 17* Hare
 18
 19The [[https://harelang.org][Hare]] programming language is a
 20straightforward language that should look familiar if you've ever
 21programmed with C, Rust, or other languages that aim to build software
 22at the system-level.
 23
 24The Hare homepage states the following:
 25
 26#+begin_quote
 27Hare is a systems programming language designed to be simple, stable,
 28and robust. Hare uses a static type system, manual memory management,
 29and minimal runtime. It is well-suited to writing operating systems,
 30system tools, compilers, networking software, and other low-level, high
 31performance tasks.
 32#+end_quote
 33
 34I have found this all to be true while playing with it for the first
 35time today. In the next few sections, I'm going to walk through my
 36installation and first program.
 37
 38** Installation
 39
 40I'm currently running Alpine Linux on my Thinkpad, so the installation
 41was quite easy as there is a package for Hare in the =apk= repositories.
 42
 43#+begin_src sh
 44doas apk add hare hare-doc
 45#+end_src
 46
 47However, I was able to install Hare from scratch on Fedora Linux a short while
 48ago, which was also very easy to do. If you need further instructions and Hare
 49doesn't have a package on your system, take a look at the [[https://harelang.org/installation/][Hare Installation]]
 50page.
 51
 52** Creating a Test Project
 53
 54In order to play with the language, I created [[https://github.com/ccleberg/learn/tree/main/hare][learn/hare]] and will be putting any
 55of my Hare-related adventures in here.
 56
 57Luckily, Hare doesn't require any complex set-up tools or build
 58environment. Once you have Hare installed, you simply need to create a
 59file ending with =.ha= and you can run a Hare program.
 60
 61I created a file called =rgb.ha= in order to test out the random number
 62generation and passing parameters between functions.
 63
 64#+begin_src sh
 65nano rgb.ha
 66#+end_src
 67
 68Within this file, I was able to easily import a few of the
 69[[https://harelang.org/tutorials/stdlib/][standard library modules]]:
 70=fmt=, =math::random=, and =datetime=.
 71
 72With these modules, I created two functions:
 73
 741. =main=: This function calls the =generate_rgb= function and then
 75   prints out the returned values.
 762. =generate_rgb=: This function uses the current Unix epoch time to
 77   generate a pseudo-random value and uses this value to create three
 78   more random values between 0 and 255. These three numbers represent a
 79   color in RGB format.
 80
 81#+begin_quote
 82*Note*: Some syntax coloring may look odd, as Zola currently doesn't
 83have a syntax highlighting theme for Hare. Instead, I'm using the C
 84theme, which may not be exactly accurate when coloring the code below.
 85#+end_quote
 86
 87#+begin_src C
 88use datetime;
 89use fmt;
 90use math::random;
 91
 92export fn main() void = {
 93    const rgb = generate_rgb();
 94    fmt::printfln("RGB: ({}, {}, {})", rgb[0], rgb[1], rgb[2])!;
 95};
 96
 97fn generate_rgb() []u64 = {
 98    // Use the current Unix epoch time as the seed value
 99    let datetime = datetime::epochunix(&datetime::now());
100
101    // Generate initial pseudo-random value
102    // You must cast the datetime from int to u64
103    let x = random::init(datetime: u64);
104
105    // Generate RGB values between (0, 255) using pseudo-random init value
106    let r = random::u64n(&x, 255);
107    let g = random::u64n(&x, 255);
108    let b = random::u64n(&x, 255);
109
110    // Structure data as array and return
111    let rgb_array: [3]u64 = [r, g, b];
112    return rgb_array;
113};
114#+end_src
115
116** Running a Program
117
118Once you have a Hare file written and ready to run, you simply need to
119run it:
120
121#+begin_src sh
122hare run file.ha
123#+end_src
124
125You can also compile the program into an executable:
126
127#+begin_src sh
128hare build -o example file.ha
129./example
130#+end_src
131
132** Initial Thoughts
133
1341. Documentation Improvements Would Help
135
136   While I was able to piece everything together eventually, the biggest
137   downfall right now in Hare's documentation. For such a new project,
138   the documentation is in a great spot. However, bare specifications
139   don't help as much as a brief examples section would.
140
141   For example, it took me a while to figure out what the =u64n=
142   function was looking for. I could tell that it took two parameters
143   and the second was my max value (255), but couldn't figure out what
144   the first value should be. Eventually, I inspected the =random.ha=
145   file in the
146   [[https://git.sr.ht/~sircmpwn/hare/tree/master/item/math/random/random.ha][Hare
147   source code]] and found the test suite that helped me discover that
148   it needed an =init()= value in the form of =&var=.
149
1502. More Basic Modules
151
152   This is another point that comes from Hare being new and awaiting
153   more contributions, but there are some basic functions that I would
154   personally enjoy seeing in Hare, such as one to convert decimal
155   (base 10) values to hexadecimal (base 16).
156
157   If I'm feeling comfortable with my math, I may work on the list of
158   functions I want and see if any can make it into the Hare source
159   code.
160
1613. Overall Thoughts
162
163   Overall, I actually really enjoy Hare. It's not as tedious to get a
164   project up and running as Rust, but it's also simpler and more
165   user-friendly than learning C. I am going to continue playing with it
166   and see if I can make anything of particular value.