list-helpers.scm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. (define-module (helpers list-helpers))
  2. (use-modules (srfi srfi-64))
  3. (define (empty-list? lst)
  4. (null? lst))
  5. (define (atom? sth)
  6. (and (not (pair? sth))
  7. (not (empty-list? sth))))
  8. (define (apply-to-items proc lst)
  9. (map proc lst))
  10. (define-public (dimendionality lst)
  11. (define (iter lst depth)
  12. (cond
  13. [(null? lst) depth]
  14. [(pair? (car lst))
  15. (max (iter (car lst) (+ depth 1))
  16. (iter (cdr lst) depth))]
  17. [else
  18. (iter (cdr lst) depth)]))
  19. (cond
  20. [(null? lst) 0]
  21. [else (iter lst 1)]))
  22. (define-public (find-longest* lst length-proc)
  23. "Find the longest thing anywhere in a potentially nested lst, where length is
  24. defined by the given length-proc."
  25. (cond
  26. [(null? lst) 0]
  27. [(pair? (car lst))
  28. (max (find-longest* (car lst) length-proc)
  29. (find-longest* (cdr lst) length-proc))]
  30. [else
  31. (max (length-proc (car lst))
  32. (find-longest* (cdr lst) length-proc))]))
  33. (define-public (longest-sublist-length lst)
  34. (cond
  35. [(null? lst) 0]
  36. [else
  37. (apply max (map length lst))]))
  38. #;(define (create-vector-length-n-from-list lst desired-length filling-element)
  39. (let ([vec-to-fill (make-vector desired-length filling-element)])
  40. (let iter ([vec-index 0] [remaining lst])
  41. (cond
  42. [(= vec-index desired-length) vec-to-fill]
  43. [(null? remaining)
  44. (vector-set! vec-to-fill vec-index filling-element)
  45. (iter (+ vec-index 1) '())]
  46. [else
  47. (vector-set! vec-to-fill vec-index (car remaining))
  48. (iter (+ vec-index 1) (cdr remaining))]))))
  49. (define (list->list-of-len lst desired-length fill-elem)
  50. (define (iter remaining elem-counter)
  51. (cond
  52. [(= elem-counter desired-length) '()]
  53. [(null? remaining)
  54. (cons fill-elem
  55. (iter '() (+ elem-counter 1)))]
  56. [else
  57. (cons (car remaining)
  58. (iter (cdr remaining) (+ elem-counter 1)))]))
  59. (iter lst 0))
  60. (define-public (stretch-list lst desired-length fill-elem)
  61. (let ([vector-to-fill (make-vector desired-length fill-elem)])
  62. (list->list-of-len lst desired-length fill-elem)))
  63. ;; (define (iter orig-lst elem-count)
  64. ;; (cond
  65. ;; [(< elem-count desired-length)
  66. ;; (cond
  67. ;; [(null? orig-lst)
  68. ;; (cons (iter '() (+ elem-count 1)) filling-elem))]
  69. ;; [else ...])]
  70. ;; [else '()]))
  71. ;; (iter lst '() 0))