From 75db8bed918453455045cc5d4dabb83a068e674e Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 29 Apr 2018 18:29:26 +0200 Subject: [PATCH] mes: filter-map: Handle two lists. * module/srfi/srfi-1.mes (filter-map): Handle two lists, add error when called with three or more. --- module/mes/base.mes | 20 ++++++++++---------- module/srfi/srfi-1.mes | 16 +++++++++++----- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/module/mes/base.mes b/module/mes/base.mes index 704ea566..51d0f488 100644 --- a/module/mes/base.mes +++ b/module/mes/base.mes @@ -107,13 +107,13 @@ ((closure? p) #t) (#t #f))) -(define (map f l . r) - (if (null? l) '() - (if (null? r) (cons (f (car l)) (map f (cdr l))) - (if (null? (cdr r)) - (cons (f (car l) (caar r)) (map f (cdr l) (cdar r))) - (if (null? (cddr r)) - (cons (f (car l) (caar r) (caadr r)) (map f (cdr l) (cdar r) (cdadr r))) - (if (null? (cdddr r)) - (cons (f (car l) (caar r) (caadr r) (car (caddr r))) (map f (cdr l) (cdar r) (cdadr r) (cdr (caddr r)))) - (error 'unsupported (cons* "map 5:" f l r))) ))))) +(define (map f h . t) + (if (null? h) '() + (if (null? t) (cons (f (car h)) (map f (cdr h))) + (if (null? (cdr t)) + (cons (f (car h) (caar t)) (map f (cdr h) (cdar t))) + (if (null? (cddr t)) + (cons (f (car h) (caar t) (caadr t)) (map f (cdr h) (cdar t) (cdadr t))) + (if (null? (cdddr t)) + (cons (f (car h) (caar t) (caadr t) (car (caddr t))) (map f (cdr h) (cdar t) (cdadr t) (cdr (caddr t)))) + (error 'unsupported (cons* "map 5:" f h t))) ))))) diff --git a/module/srfi/srfi-1.mes b/module/srfi/srfi-1.mes index 321a47f5..d5103390 100644 --- a/module/srfi/srfi-1.mes +++ b/module/srfi/srfi-1.mes @@ -40,11 +40,17 @@ (define (append-map f lst . rest) (apply append (apply map f (cons lst rest)))) -(define (filter-map f lst) - (if (null? lst) (list) - (let ((r (f (car lst)))) - (if r (cons r (filter-map f (cdr lst))) - (filter-map f (cdr lst)))))) +(define (filter-map f h . t) + (if (null? h) '() + (if (null? t) + (let ((r (f (car h)))) + (if r (cons r (filter-map f (cdr h))) + (filter-map f (cdr h)))) + (if (null? (cdr t)) + (let ((r (f (car h) (caar t)))) + (if r (cons r (filter-map f (cdr h) (cdar t))) + (filter-map f (cdr h) (cdar t)))) + (error 'unsupported (cons* "filter-map 3:" f h t)))))) ;;; nyacc requirements