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. (define (dots? x) (eqv? 'dots (car x)))
  85.  
  86. (define (dot-count x) (caadr x))
  87.  
  88. ;; Function to gather and tag tokens based on dots
  89. (define (process-final tokens)
  90. (define (process-helper reversed-tokens forward-tail)
  91. (cond
  92. ((null? reversed-tokens)
  93. (simplify-for-output forward-tail))
  94. ((dots? (car reversed-tokens))
  95. (let* ((count (dot-count (car reversed-tokens)))
  96. (collected (take count forward-tail))
  97. (the-rest (drop count forward-tail))
  98. (the-tail (append
  99. collected
  100. (list
  101. (tag-token 'right-parenthesis ")"))
  102. the-rest)))
  103. the-tail))
  104. (else (process-helper
  105. (cdr reversed-tokens)
  106. (cons (car reversed-tokens) forward-tail)))))
  107. (process-helper (reverse tokens) '()))
  108.  
  109.  
  110. ;; Example usage
  111. (define input "1 2 3")
  112.  
  113. ;; Tokenize the input
  114. (let ((tokens (tokenize input)))
  115. (write tokens) ; Write the intermediate tokenized output for debugging
  116. (newline)(newline)
  117. (write (process-final tokens))) ; Return the final processed output
  118.  
Success #stdin #stdout 0.02s 7992KB
stdin
Standard input is empty
stdout
((other (#\1)) (other (#\2)) (other (#\3)))

((other (#\1)) (other (#\2)) (other (#\3)))