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 working)
  87. (cond
  88. ((null? tokens) (simplify-for-output working))
  89. ((equal? (car (car tokens)) 'dots)
  90. (let* ((count (cadr (car tokens)))
  91. (gathered (cons 'final (take count working)))
  92. (remaining (drop count working)))
  93. (process-final (cons gathered remaining))))
  94. (else
  95. (process-helper (cdr tokens) (tag-token 'final (car tokens))))))
  96. (process-helper (reverse tokens) '()))
  97.  
  98. ;; Example usage
  99. (define input "1 2")
  100.  
  101. ;; Tokenize the input
  102. (let ((tokens (tokenize input)))
  103. (write tokens) ; Write the intermediate tokenized output for debugging
  104. (newline)(newline)
  105. (write (process-final tokens))) ; Return the final processed output
  106.  
Success #stdin #stdout 0.01s 7996KB
stdin
Standard input is empty
stdout
((other (#\1)) (other (#\2)))

(final (other (#\1)))