util.scm 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. ;;;; Copyright (C) 1999, 2000, 2001, 2003, 2006, 2008, 2012 Free Software Foundation, Inc.
  2. ;;;;
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;;;
  17. (define-module (oop goops util)
  18. :export (mapappend find-duplicate
  19. map* for-each* length* improper->proper)
  20. :use-module (srfi srfi-1)
  21. :re-export (any every)
  22. :no-backtrace
  23. )
  24. ;;;
  25. ;;; {Utilities}
  26. ;;;
  27. (define mapappend append-map)
  28. (define (find-duplicate l) ; find a duplicate in a list; #f otherwise
  29. (cond
  30. ((null? l) #f)
  31. ((memv (car l) (cdr l)) (car l))
  32. (else (find-duplicate (cdr l)))))
  33. (define (map* fn . l) ; A map which accepts dotted lists (arg lists
  34. (cond ; must be "isomorph"
  35. ((null? (car l)) '())
  36. ((pair? (car l)) (cons (apply fn (map car l))
  37. (apply map* fn (map cdr l))))
  38. (else (apply fn l))))
  39. (define (for-each* fn . l) ; A for-each which accepts dotted lists (arg lists
  40. (cond ; must be "isomorph"
  41. ((null? (car l)) '())
  42. ((pair? (car l)) (apply fn (map car l)) (apply for-each* fn (map cdr l)))
  43. (else (apply fn l))))
  44. (define (length* ls)
  45. (do ((n 0 (+ 1 n))
  46. (ls ls (cdr ls)))
  47. ((not (pair? ls)) n)))
  48. (define (improper->proper ls)
  49. (if (pair? ls)
  50. (cons (car ls) (improper->proper (cdr ls)))
  51. (list ls)))