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 for Dot Count
  39. (define (process-dots chars)
  40. (let* ((dot-chars (take-while (lambda (ch) (char=? ch (integer->char 46))) chars)) ; char 46 is '.'
  41. (num-dots (length dot-chars))
  42. (rest (drop-while (lambda (ch) (char=? ch (integer->char 46))) chars)))
  43. (list num-dots rest)))
  44.  
  45. ;; Define main tokenize function with integrated new functions
  46. (define (tokenize input)
  47. (let loop ((chars (string->list input)) (tokens '()))
  48. (cond
  49. ((null? chars) (reverse tokens)) ; Return the reversed tokens
  50. ((char=? (car chars) (integer->char 91)) ; Handle opening bracket [
  51. (loop (cdr chars) (cons (tag-token 'open-bracket "[") tokens)))
  52. ((char=? (car chars) (integer->char 93)) ; Handle closing bracket ]
  53. (loop (cdr chars) (cons (tag-token 'close-bracket "]") tokens)))
  54. ((char=? (car chars) (integer->char 40)) ; Handle opening parenthesis
  55. (loop (cdr chars) (cons (tag-token 'open-parenthesis "(") tokens)))
  56. ((char=? (car chars) (integer->char 41)) ; Handle closing parenthesis
  57. (loop (cdr chars) (cons (tag-token 'close-parenthesis ")") tokens)))
  58. ((char=? (car chars) (integer->char 32)) ; Skip spaces
  59. (loop (cdr chars) tokens))
  60. ((char=? (car chars) (integer->char 46)) ; Handle dots (.)
  61. (let* ((result (process-dots chars))
  62. (num-dots (car result))
  63. (rest (cadr result)))
  64. (loop rest (cons (tag-token 'dots num-dots) tokens)))) ; Use a tagged list structure
  65. (else
  66. (let* ((result (process-other chars))
  67. (token (car result))
  68. (rest (cadr result)))
  69. (loop rest (cons (tag-token 'other token) tokens))))))) ; End of tokenize function
  70.  
  71. ;; Example usage
  72. (define input "(define) f g (). 1 2")
  73.  
  74. ;; Tokenize the input
  75. (let ((tokens (tokenize input)))
  76. (write tokens) ; Write the intermediate tokenized output for debugging
  77. (newline)
  78. tokens) ; Return the intermediate tokenized output
  79.  
  80.  
Success #stdin #stdout 0.01s 8056KB
stdin
Standard input is empty
stdout
((open-parenthesis "(") (other (#\d #\e #\f #\i #\n #\e)) (close-parenthesis ")") (other (#\f)) (other (#\g)) (open-parenthesis "(") (close-parenthesis ")") (dots 1) (other (#\1)) (other (#\2)))