blob: 3863f02a3ce6bc1719ad7d14a7722943136d7687 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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)))
|