list-utils.scm 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. (define-module (list-utils)
  2. #:export (list-prefix?
  3. multirember*-with-equal-proc
  4. multirember-with-equal-proc
  5. multirember-equal
  6. multirember*-equal))
  7. (define list-prefix?
  8. (λ (lst lst-prefix)
  9. (cond
  10. [(null? lst-prefix) #t]
  11. [(null? lst) #f]
  12. [else
  13. (cond
  14. [(equal? (car lst) (car lst-prefix))
  15. (list-prefix? (cdr lst) (cdr lst-prefix))]
  16. [else #f])])))
  17. ;; scheme@(guile-user)> (list-prefix? '(a b c) '(a b))
  18. ;; $11 = #t
  19. ;; scheme@(guile-user)> (list-prefix? '(a b c) '(a b c))
  20. ;; $12 = #t
  21. ;; scheme@(guile-user)> (list-prefix? '(a b c) '(a b c d))
  22. ;; $13 = #f
  23. ;; scheme@(guile-user)> (list-prefix? '(a b c) '(a))
  24. ;; $14 = #t
  25. ;; scheme@(guile-user)> (list-prefix? '(a b c) '(b))
  26. ;; $15 = #f
  27. ;; scheme@(guile-user)> (list-prefix? '(a b c) '())
  28. ;; $16 = #t
  29. ;; scheme@(guile-user)> (list-prefix? '(a b c) '())
  30. (define multirember*-with-equal-proc
  31. (λ (equal-proc)
  32. (λ (lst unwanted)
  33. (let loop ([remaining-list lst])
  34. (cond
  35. [(null? remaining-list)
  36. '()]
  37. ;; case for finding the unwanted element in the list
  38. [(equal-proc (car remaining-list) unwanted)
  39. (loop (cdr remaining-list))]
  40. ;; case for handling nested lists
  41. [(pair? (car remaining-list))
  42. (cons (loop (car remaining-list))
  43. (loop (cdr remaining-list)))]
  44. [else
  45. (cons (car remaining-list)
  46. (loop (cdr remaining-list)))])))))
  47. (define multirember-with-equal-proc
  48. (λ (equal-proc)
  49. (λ (lst unwanted)
  50. (let loop ([remaining-list lst])
  51. (cond
  52. [(null? remaining-list)
  53. '()]
  54. [(equal-proc (car remaining-list) unwanted)
  55. (loop (cdr remaining-list))]
  56. [else
  57. (cons (car remaining-list)
  58. (loop (cdr remaining-list)))])))))
  59. (define multirember-equal
  60. (multirember-with-equal-proc equal?))
  61. (define multirember*-equal
  62. (multirember*-with-equal-proc equal?))
  63. ;; (define list-left-trim-with-equality-proc
  64. ;; (λ (equality-proc)
  65. ;; (λ (lst unwanted)
  66. ;; (let loop ([remaining-list lst])
  67. ;; (cond
  68. ;; [(null? remaining-list) '()]
  69. ;; ;; case for finding the unwanted element in the list
  70. ;; [(equality-proc (car remaining-list) unwanted)
  71. ;; (loop (cdr remaining-list))]
  72. ;; ;; case for handling nested lists
  73. ;; [(pair? (car remaining-list))
  74. ;; remaining-list]
  75. ;; [else
  76. ;; ;; prefix of unwanted elements ended
  77. ;; remaining-list])))))
  78. ;; (define list-left-trim-empty-strings
  79. ;; (λ (lst-of-str)
  80. ;; ((list-left-trim-with-equality-proc string=?) lst-of-str "")))