From e61508e4764d69a4ea0073f419b89ff0a06e813e Mon Sep 17 00:00:00 2001 From: bd Date: Sun, 9 Jun 2024 22:02:45 -0600 Subject: AoC 2015.1 p1+2 --- not-quite-lisp/nql.scm | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 not-quite-lisp/nql.scm (limited to 'not-quite-lisp/nql.scm') diff --git a/not-quite-lisp/nql.scm b/not-quite-lisp/nql.scm new file mode 100644 index 0000000..3863f02 --- /dev/null +++ b/not-quite-lisp/nql.scm @@ -0,0 +1,33 @@ +(define-module (nql) + #:use-module (srfi srfi-13) + #:export (nql + paren->number)) + + +(define (nql str count-all?) + "GIven string of parens STR, +calculates '(' - ')' when +COUNT-ALL is #t.. + +If COUNT-ALL? is #f, returns the +index of the first time a negative +number is reached." + (let loop ((floor 0) (iteration 0) (lst (map paren->number + (string->list str)))) + (cond + ((and (not count-all?) + (> 0 floor)) iteration) + ((null? lst) (and count-all? floor)) + (#t (loop (+ (car lst) floor) + (1+ iteration) + (cdr lst)))))) + +(define (paren->number char) + "Given a character representing a +parenthesis, returns 1 for '(' and +-1 for ')'. +Returns zero for all other characters." + (cond + ((char=? char #\() 1) + ((char=? char #\)) -1) + (#t 0))) -- cgit v1.2.3