From c16fecddcbae5c05595318f5b2c2105b9fe514d7 Mon Sep 17 00:00:00 2001 From: Brady McDonough Date: Thu, 7 Jan 2021 14:14:00 -0700 Subject: [PATCH] Added (flatten lst) and guile module definition --- tk/listlogic.scm | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tk/listlogic.scm b/tk/listlogic.scm index 4f0dd75..8fdd602 100644 --- a/tk/listlogic.scm +++ b/tk/listlogic.scm @@ -1,3 +1,21 @@ +(define-module (tk listlogic) + #:export ('flatten + 'lp& + 'l& + ) + #:duplicates (warn-override-core warn )) + +(define (flatten-helper lst acc stk) + (cond ((null? lst) + (if (null? stk) (reverse acc) + (flatten-helper (car stk) acc (cdr stk)))) + ((pair? (car lst)) + (flatten-helper (car lst) acc (cons (cdr lst) stk))) + (else + (flatten-helper (cdr lst) (cons (car lst) acc) stk)))) + +(define (flatten lst) (flatten-helper lst '() '())) + (define (lp& p ls1 ls2) (let loop ((ls1 ls1) (ls2 ls2)) (cond ((not (and (pair? ls1) (pair? ls2))) @@ -7,8 +25,6 @@ (else '()) ))) -(define (ls& ls1 ls2) +(define (l& ls1 ls2) (cond ((eqv? ls1 ls2) ls1) (else (lp& eqv? ls1 ls2)))) - -