libraries.scm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. (define (fake-open-input-file filename)
  2. (define p (assoc filename file-system-level-data))
  3. (define s (cdr p))
  4. (open-input-string s))
  5. (define (string->vector s)
  6. (list->vector (string->list s)))
  7. (define (vector->string v)
  8. (list->string (vector->list v)))
  9. (define (vector-map lam . vs)
  10. (list->vector (apply map lam (map vector->list vs))))
  11. (define (vector-copy v)
  12. (list->vector (list-copy (vector->list v))))
  13. (define (list-copy l)
  14. (let loop ((in l)
  15. (out '()))
  16. (if (null? in)
  17. (reverse out)
  18. (loop (cdr in)
  19. (cons (car in) out)))))
  20. (define (string-take s nchars)
  21. (let loop ((in (string->list s))
  22. (out '())
  23. (i 0))
  24. (if (or (null? in)
  25. (= i nchars))
  26. (list->string (reverse out))
  27. (loop (cdr in)
  28. (cons (car in) out)
  29. (+ i 1)))))
  30. (define (string-split-with-args s delimiter)
  31. (let loop ((in (string->list s))
  32. (out '(())))
  33. (if (null? in)
  34. (reverse (map (lambda (soc) (list->string (reverse soc))) out))
  35. (loop (cdr in)
  36. (if (char=? (car in) (string-ref delimiter 0))
  37. (cons '() out)
  38. (cons (cons (car in) (car out))
  39. (cdr out)))))))
  40. (define (string-split . args)
  41. (define sz (length args))
  42. (cond
  43. ((< sz 1) (error "not enough arguments"))
  44. ((= sz 1)
  45. (string-split-with-args (list-ref args 0)
  46. " "))
  47. ((= sz 2)
  48. (string-split-with-args (list-ref args 0)
  49. (list-ref args 1)))
  50. (else
  51. (error "Too many arguments"))))
  52. (define (string-join-with-args string-list delimiter)
  53. (cond
  54. ((null? string-list)
  55. "")
  56. ((null? (cdr string-list))
  57. (car string-list))
  58. (else
  59. (let loop ((in string-list)
  60. (out '()))
  61. (if (null? in)
  62. (apply string-append (reverse (cdr out)))
  63. (loop (cdr in)
  64. (cons delimiter
  65. (cons (car in)
  66. out))))))))
  67. (define (string-join . args)
  68. (define sz (length args))
  69. (cond
  70. ((< sz 1) (error "not enough arguments"))
  71. ((= sz 1)
  72. (string-join-with-args (list-ref args 0)
  73. " "))
  74. ((= sz 2)
  75. (string-join-with-args (list-ref args 0)
  76. (list-ref args 1)))
  77. (else
  78. (error "Too many arguments"))))
  79. (define (list-pad-right-with-args s len char)
  80. (let loop ((in s)
  81. (out '())
  82. (i 0))
  83. (if (= i len)
  84. (reverse out)
  85. (loop (if (null? in)
  86. '()
  87. (cdr in))
  88. (cons (if (null? in)
  89. char
  90. (car in))
  91. out)
  92. (+ i 1)))))
  93. (define (string-pad-right . args)
  94. (define sz (length args))
  95. (cond
  96. ((< sz 2) (error "not enough arguments"))
  97. ((= sz 2)
  98. (list->string
  99. (list-pad-right-with-args (string->list (list-ref args 0))
  100. (list-ref args 1)
  101. #\space)))
  102. ((= sz 3)
  103. (list->string
  104. (list-pad-right-with-args (string->list (list-ref args 0))
  105. (list-ref args 1)
  106. (list-ref args 2))))
  107. (else
  108. (error "Too many arguments"))))
  109. (define (list-pad-left-with-args s len char)
  110. (reverse (list-pad-right-with-args (reverse s) len char)))
  111. (define (string-pad-left . args)
  112. (define sz (length args))
  113. (cond
  114. ((< sz 2) (error "not enough arguments"))
  115. ((= sz 2)
  116. (list->string
  117. (list-pad-left-with-args (string->list (list-ref args 0))
  118. (list-ref args 1)
  119. #\space)))
  120. ((= sz 3)
  121. (list->string
  122. (list-pad-left-with-args (string->list (list-ref args 0))
  123. (list-ref args 1)
  124. (list-ref args 2))))
  125. (else
  126. (error "Too many arguments"))))
  127. (define (iota n)
  128. (let loop ((result '())
  129. (i 0))
  130. (if (< i n)
  131. (loop (cons i result)
  132. (+ i 1))
  133. (reverse result))))