list-utils.scm 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. (library (list-utils)
  2. (export list-prefix?
  3. make-multiple-recursive-list-remover
  4. make-multiple-list-remover
  5. list-remove-multiple-recursive-equal
  6. list-remove-multiple-recursive-eqv
  7. list-remove-multiple-recursive-eq
  8. list-remove-multiple-equal
  9. list-remove-multiple-eqv
  10. list-remove-multiple-eq
  11. map-to-all-except-last)
  12. (import
  13. (except (rnrs base) let-values)
  14. (only (guile)
  15. lambda* λ)))
  16. (define list-prefix?
  17. (λ (lst lst-prefix)
  18. (cond
  19. [(null? lst-prefix) #t]
  20. [(null? lst) #f]
  21. [else
  22. (cond
  23. [(equal? (car lst) (car lst-prefix))
  24. (list-prefix? (cdr lst) (cdr lst-prefix))]
  25. [else #f])])))
  26. (define make-multiple-recursive-list-remover
  27. ;; multirember*-with-equal-proc
  28. (λ (equal-proc)
  29. (λ (lst unwanted)
  30. (let loop ([remaining-list lst])
  31. (cond
  32. [(null? remaining-list)
  33. '()]
  34. ;; case for finding the unwanted element in the list
  35. [(equal-proc (car remaining-list) unwanted)
  36. (loop (cdr remaining-list))]
  37. ;; case for handling nested lists
  38. [(pair? (car remaining-list))
  39. (cons (loop (car remaining-list))
  40. (loop (cdr remaining-list)))]
  41. [else
  42. (cons (car remaining-list)
  43. (loop (cdr remaining-list)))])))))
  44. (define make-multiple-list-remover
  45. (λ (equal-proc)
  46. (λ (lst unwanted)
  47. (let loop ([remaining-list lst])
  48. (cond
  49. [(null? remaining-list)
  50. '()]
  51. [(equal-proc (car remaining-list) unwanted)
  52. (loop (cdr remaining-list))]
  53. [else
  54. (cons (car remaining-list)
  55. (loop (cdr remaining-list)))])))))
  56. (define list-remove-multiple-recursive-equal
  57. (make-multiple-recursive-list-remover equal?))
  58. (define list-remove-multiple-recursive-eqv
  59. (make-multiple-recursive-list-remover eqv?))
  60. (define list-remove-multiple-recursive-eq
  61. (make-multiple-recursive-list-remover eq?))
  62. (define list-remove-multiple-equal
  63. (make-multiple-list-remover equal?))
  64. (define list-remove-multiple-eqv
  65. (make-multiple-list-remover eqv?))
  66. (define list-remove-multiple-eq
  67. (make-multiple-list-remover eq?))
  68. (define map-to-all-except-last
  69. (λ (proc lst)
  70. (cond
  71. [(null? lst) '()]
  72. [(null? (cdr lst)) lst]
  73. [else
  74. (cons (proc (car lst))
  75. (map-to-all-except-last proc (cdr lst)))])))