Skip to content
TheChatArchiveThe Chat Archive
Perplexitysonar-reasoning· PerplexityHugging Face LMArena Search Arena 24K

how to iterate over a string in gleam? be concise.

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Perplexitysonar-reasoning-pro-high’s answer.
  1. User

    how to iterate over a string in gleam? be concise.

  2. Perplexity Assistant

    In Gleam, strings are UTF-8 encoded and iterated using grapheme clusters. Two common approaches:

    1. Recursive iteration with pop_grapheme:

    pub fn process_string(str: String) {
      case string.pop_grapheme(str) {
        Ok(#(head, tail)) -> {
          // Process head grapheme
          process_string(tail)
        }
        Error(Nil) -> Nil // Base case
      }
    }
    

    This splits strings into head/tail pairs until empty[1][2].

    2. Pattern matching with <> operator:

    case my_string {
      "prefix" <> rest -> // Handle rest
      first_grapheme <> rest -> // Process first grapheme
      "" -> // Empty case
    }
    

    Useful for matching prefixes/suffixes while binding remaining characters[7].

    For index-based access, use slice(from: _, at_index: _, length: _) with negative indices supported[1][2]. Remember strings are immutable - operations create new strings.

  3. User

    be concise please. how to write a case?

  4. Perplexity Assistant
    case my_string {
      "" -> // Handle empty string
      "a" <> rest -> // Handle 'a' prefix
      first <> rest -> // Process first grapheme
    }
    

    Patterns match:

    • "" for empty
    • "prefix" <> rest for specific starts
    • first <> rest splits first grapheme from remainder

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.