util.scm 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ;;;; Copyright (C) 1999, 2000, 2001, 2003, 2006, 2008 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 2.1 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 top-level-env top-level-env?
  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 (top-level-env)
  34. (let ((mod (current-module)))
  35. (if mod
  36. (module-eval-closure mod)
  37. '())))
  38. (define (top-level-env? env)
  39. (or (null? env)
  40. (procedure? (car env))))
  41. (define (map* fn . l) ; A map which accepts dotted lists (arg lists
  42. (cond ; must be "isomorph"
  43. ((null? (car l)) '())
  44. ((pair? (car l)) (cons (apply fn (map car l))
  45. (apply map* fn (map cdr l))))
  46. (else (apply fn l))))
  47. (define (for-each* fn . l) ; A for-each which accepts dotted lists (arg lists
  48. (cond ; must be "isomorph"
  49. ((null? (car l)) '())
  50. ((pair? (car l)) (apply fn (map car l)) (apply for-each* fn (map cdr l)))
  51. (else (apply fn l))))
  52. (define (length* ls)
  53. (do ((n 0 (+ 1 n))
  54. (ls ls (cdr ls)))
  55. ((not (pair? ls)) n)))
  56. (define (improper->proper ls)
  57. (if (pair? ls)
  58. (cons (car ls) (improper->proper (cdr ls)))
  59. (list ls)))