I have only ever used high level languages but am trying to rewrite some of the algorithms from CLRS in Zig. So far going terribly, but quite fun.
I need to read docs on how to actually compile so this is my best guess at syntax for the naive string matching algorithm (p. 988, CLRS). I plan to write a little bit most days so hopefully tomorrow will have read through docs and understand how to build and try code below.
const std = @import("std");
pub fn main() void {
fn naiveString(the_string: [_]u8, the_pattern: [_]u8) {
var n: u8 = the_string.len;
var m: u8 = the_pattern.len;
var upper_lim: u8 = n-m+1;
var start: u8 = 0;
while (i < upper_lim): (i+=1) {
if (the_pattern == the_string[i..i+m]) {
return std.log.info(i)
}
}
}
}
Current uncertainties/questions:
- No ranges to iterate over with for loops, is this to prevent getting range wrong?
- First page on strings I read mentions pointers, and I donāt know working with pointers so this is worrying
- How to decide which primitive type to use, so many options
- So much I take for granted in Python