Strings and Runes in Scheme

;; A Scheme string is a sequence of characters. Unlike Go,
;; Scheme doesn't have a separate concept of runes or UTF-8 encoding.
;; Strings in Scheme are typically treated as sequences of characters,
;; and the exact representation may depend on the implementation.

;; Define a string containing the Thai word for "hello"
(define s "สวัสดี")

;; Print the length of the string
(display "Len: ")
(display (string-length s))
(newline)

;; In Scheme, we don't have direct byte access to strings.
;; Instead, we can iterate over the characters:
(for-each (lambda (c)
            (display (char->integer c))
            (display " "))
          (string->list s))
(newline)

;; Count the number of characters in the string
(display "Character count: ")
(display (string-length s))
(newline)

;; Iterate over the characters in the string
(let loop ((i 0))
  (when (< i (string-length s))
    (let ((c (string-ref s i)))
      (display "Character ")
      (display c)
      (display " is at index ")
      (display i)
      (newline))
    (loop (+ i 1))))

;; Define a function to examine a character
(define (examine-char c)
  (cond
   ((char=? c #\t) (display "found tee"))
   ((char=? c #\ส) (display "found so sua"))
   (else (display "found other character")))
  (newline))

;; Call examine-char for each character in the string
(for-each examine-char (string->list s))

This Scheme code demonstrates working with strings and characters, which is the closest equivalent to Go’s concept of strings and runes. Here are some key points:

  1. Scheme doesn’t distinguish between bytes and characters in strings. Strings are sequences of characters.

  2. We use string-length to get the length of a string, which returns the number of characters.

  3. There’s no direct byte-level access to strings in Scheme. Instead, we can convert the string to a list of characters and iterate over them.

  4. Scheme doesn’t have a built-in concept of Unicode code points. Character handling depends on the implementation.

  5. We use string-ref to access individual characters in a string by index.

  6. The examine-char function demonstrates how to compare characters. Note that the exact representation of non-ASCII characters may vary between Scheme implementations.

To run this Scheme code, you would typically save it to a file (e.g., strings-and-chars.scm) and then use a Scheme interpreter or compiler to execute it. The exact command may vary depending on your Scheme implementation, but it might look something like this:

$ scheme --script strings-and-chars.scm

Note that the output may vary depending on how your Scheme implementation handles Unicode characters and how it represents character codes.