[ prog / sol / mona ]

prog


Zig

5 2023-10-15 14:20

Is there a comptime lisp interperter in zig? I noticed that S-Expressions can
be embbeded in zig quite easly, for instance:

Scheme:

(define (power b e)
    (define (recursive_step b e accum)
        (cond 
            ((= e 0) accum)
            ((even? e) (recursive_step (* b b) (/ e 2) accum      ))
            (else      (recursive_step b       (- e 1) (* accum b)))))
    (recursive_step b e 1))

Zig:

.{ .define, .{ .power, .b, .e }, 
    .{ .define, .{ .recursive_step, .b, .e, .accum },
        .{ .cond,
            .{ .{ .equals, .e, 0 }, .accum },
            .{ .{ .isEven, .e }, .{ .recursive_step, .{ .mul, .b, .b }, .{ .div, .e, 2 }, .accum } },
            .{ .Else, .{ .recursive_step, .b, .{ .sub, .e, 1 }, .{ .mul, .accum, .b } } },
        },
    }, 
.{ .recursive_step, .b, .e, 1 } };

With some minor alterations due to not beeing able to have .else and .= .+ and etc

30


VIP:

do not edit these