fork(1) download
  1. ;; Define helper functions
  2. (define (drop-while pred lis)
  3. (if (or (null? lis) (not (pred (car lis))))
  4. lis
  5. (drop-while pred (cdr lis))))
  6.  
  7. (define (take-while pred lis)
  8. (define (take-while-helper pred lis result)
  9. (if (and (not (null? lis)) (pred (car lis)))
  10. (take-while-helper pred (cdr lis) (cons (car lis) result))
  11. (reverse result)))
  12. (take-while-helper pred lis '()))
  13.  
  14. (define (char-other? ch)
  15. (not (or (= (char->integer ch) 91)
  16. (= (char->integer ch) 93)
  17. (= (char->integer ch) 40)
  18. (= (char->integer ch) 41)
  19. (= (char->integer ch) 32)
  20. (= (char->integer ch) 10))))
  21.  
  22. (define (process-other chars)
  23. (and (not (null? chars))
  24. (let* ((token-chars (take-while char-other? chars))
  25. (rest (drop-while char-other? chars)))
  26. (list token-chars rest))))
  27.  
  28. ;; Custom function to check if all elements in a list are characters
  29. (define (all-char? lis)
  30. (if (null? lis)
  31. #t
  32. (and (char? (car lis)) (all-char? (cdr lis)))))
  33.  
  34. ;; Function to tag tokens with their type
  35. (define (tag-token type value)
  36. (list type value))
  37.  
  38. ;; Functions to take and drop elements from the list
  39. (define (take n lis)
  40. (if (or (<= n 0) (null? lis))
  41. '()
  42. (cons (car lis) (take (- n 1) (cdr lis)))))
  43.  
  44. (define (drop n lis)
  45. (if (or (<= n 0) (null? lis))
  46. lis
  47. (drop (- n 1) (cdr lis))))
  48.  
  49. ;; Functions for Dot Count
  50. (define (process-dots chars)
  51. (let* ((dot-chars (take-while (lambda (ch) (char=? ch (integer->char 46))) chars)) ; char 46 is '.'
  52. (num-dots (length dot-chars))
  53. (rest (drop-while (lambda (ch) (char=? ch (integer->char 46))) chars)))
  54. (list num-dots rest)))
  55.  
  56. ;; Define main tokenize function with integrated new functions
  57. (define (tokenize input)
  58. (let loop ((chars (string->list input)) (tokens '()))
  59. (cond
  60. ((null? chars) (reverse tokens)) ; Return the reversed tokens
  61. ((char=? (car chars) (integer->char 91)) ; Handle opening bracket [
  62. (loop (cdr chars) (cons (tag-token 'open-bracket "[") tokens)))
  63. ((char=? (car chars) (integer->char 93)) ; Handle closing bracket ]
  64. (loop (cdr chars) (cons (tag-token 'close-bracket "]") tokens)))
  65. ((char=? (car chars) (integer->char 40)) ; Handle opening parenthesis
  66. (loop (cdr chars) (cons (tag-token 'open-parenthesis "(") tokens)))
  67. ((char=? (car chars) (integer->char 41)) ; Handle closing parenthesis
  68. (loop (cdr chars) (cons (tag-token 'close-parenthesis ")") tokens)))
  69. ((char=? (car chars) (integer->char 32)) ; Skip spaces
  70. (loop (cdr chars) tokens))
  71. ((char=? (car chars) (integer->char 46)) ; Handle dots (.)
  72. (let* ((result (process-dots chars))
  73. (num-dots (car result))
  74. (rest (cadr result)))
  75. (loop rest (cons (tag-token 'dots num-dots) tokens)))) ; Use a tagged list structure
  76. (else
  77. (let* ((result (process-other chars))
  78. (token (car result))
  79. (rest (cadr result)))
  80. (loop rest (cons (tag-token 'other token) tokens)))))) ) ; End of tokenize function
  81.  
  82. (define (simplify-for-output x) x)
  83.  
  84. ;; Function to gather and tag tokens based on dots
  85. (define (process-final tokens)
  86. (define (process-helper tokens-reversed forward-tokens)
  87. (newline)(write (list 'tr tokens-reversed 'ft forward-tokens))(newline)
  88. (cond
  89. ((null? tokens-reversed) (simplify-for-output forward-tokens))
  90. ((equal? (car (car tokens-reversed)) 'dots)
  91. (let* ((count (cadr (car tokens-reversed)))
  92. (gathered (take count forward-tokens))
  93. (remaining (drop count forward-tokens)))
  94. (write (list 'gathered gathered '- 'remaining remaining))))
  95. (else (append (cdr tokens-reversed) (list (car tokens-reversed))))
  96. ))
  97.  
  98. (process-helper (reverse tokens) '()))
  99.  
  100. ;; Example usage
  101. (define input "abs x")
  102.  
  103. ;; Tokenize the input
  104. (let ((tokens (tokenize input)))
  105. (write tokens) ; Write the intermediate tokenized output for debugging
  106. (newline)(newline)
  107. (write (process-final tokens))) ; Return the final processed output
  108.  
Success #stdin #stdout 0.02s 7932KB
stdin
Standard input is empty
stdout
((other (#\a #\b #\s)) (other (#\x)))


(tr ((other (#\x)) (other (#\a #\b #\s))) ft ())
((other (#\a #\b #\s)) (other (#\x)))