I’m trying to implement the HIFF function, which requires a recursive subfunction like this one:
pub fn T(ev: []u8, result: []u8) void {
if (std.mem.eql(u8, ev, "-") or std.mem.eql(u8, ev, "0") or std.mem.eql(u8, ev, "1")) {
result = ev;
} else if (std.mem.eql(u8, ev, "00")) {
result = "0";
} else if (std.mem.eql(u8, ev, "11")) {
result = "1";
} else if (std.mem.eql(u8, ev, "01") or std.mem.eql(u8, ev, "10")) {
result = "-";
} else if ((ev.len == 2) and std.mem.indexOf(u32, ev, "-")) {
result = "-";
} else {
result = t(T(ev[0 .. ev.len / 2], result), T(ev[ev.len / 2 .. ev.len], result));
}
}
Essentially, it looks at a string and produces another, recursing in certain conditions.
Problem is, I can’t figure out how to return the string. This is the last installment, that has included returning u8, using *[u8] as a pass-by-value; in some cases it’s because it’s constant, others it’s another thing.
Essentially I would like to know how to do something like
fun a(b: []u8) []u8 {
if ( std.mem.eql(u8,b,"foo") {
return "This is foo";
else {
return "This is bar";
}
}