bkataru

basic algo care with hare

haphazard collection of programs written in the hare programming language (a.k.a. harelang) as solutions to simple problems and/or implementations of basic algorithms.

the problem statements themselves were either taken from or inspired by similar ones in:

get yourself a 🐇

you can bootstrap the hare toolchain in any linux environment/distro/machine as follows:

  1. bootstrap the hare compiler with its QBE backend, i.e., harec
$ git clone https://git.sr.ht/~sircmpwn/harec && cd harec
$ cp configs/linux.mk config.mk
$ make

# run the test suite
$ make check

# install system wide
$ make install
  1. bootstrap the hare and haredoc command, i.e., the build driver, the stdlib, and its documentation
$ git clone https://git.sr.ht/~sircmpwn/hare && cd hare
$ cp configs/linux.mk config.mk
$ make

# run the test suite
$ make check

# install system wide
$ make install

this will install the harec, hare, and haredoc binaries from the toolchain into /usr/local/bin, which you can verify with

$ hare version
hare 0.24.2-rc1-412-gf2d0f47d

1. triple sum condition

write a hare program to compute the sum of two input values. if the two values are the same, then return triple their sum.

use os;
use fmt;
use bufio;
use strings;
use strconv;

fn triplesumcondition(a: int, b: int) int = {
        const sum = a + b;
        return if (a == b) 3 * sum else sum;
};

export fn main() void = {
        fmt::println("Enter the first value: ")!;
        const a = bufio::read_line(os::stdin)! as []u8;
        const a = strings::fromutf8(a)!;
        const a = strconv::stoi(a)!;

        fmt::println("Enter the second value: ")!;
        const b = bufio::read_line(os::stdin)! as []u8;
        const b = strings::fromutf8(b)!;
        const b = strconv::stoi(b)!;

        fmt::printfln("sum of {} and {} = {}", a, b, triplesumcondition(a, b))!;
};

2. absolute difference from 51

write a hare program that will take a number as input and find the absolute difference between the input number and 51. if the input number is greater than 51, it will return triple the absolute difference.

use os;
use fmt;
use bufio;
use strings;
use strconv;

fn abs(n: int) int = {
	const diff = n - 51;
	const diff = if (diff > 0) 3 * diff else -diff;
	return diff;
};

export fn main() void = {
	fmt::println("Enter the number: ")!;
	const num = bufio::read_line(os::stdin)! as []u8;
	const num = strings::fromutf8(num)!;
	const num = strconv::stoi(num)!;

	fmt::printfln("{}", abs(num))!;
};

3. all but one

given an array of integers, write a hare program that returns a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.

for example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. if our input was [3, 2, 1], the expected output would be [2, 3, 6].

use fmt;

export fn main() void = {
	const arr: [_]int = [1, 2, 3, 4, 5];
	const new = alloc([1...], len(arr))!;
	defer free(new);

	for (let i = 0z; i < len(arr); i += 1) {
		for (let j = 0z; j < len(arr); j += 1) {
			if (i == j) continue;
			new[i] *= arr[j];
		};
	};

	for (let elem .. new) fmt::printfln("{}", elem)!;
};