Home > Software design >  Maximum length of string literals in Scheme
Maximum length of string literals in Scheme

Time:01-15

Is there a maximum length for string literals in Scheme? Or is it dependent on Scheme implementation? In C, the standards specify the maximum length of string literals for portable programs. Is there a similar specification for Scheme?

CodePudding user response:

It is implementation defined.

This does not result in direct way as in ISO9899 but by deduction.

In the small report, in 6.2.3. Implementation restrictions, we can see:

An implementation of Scheme must support exact in- tegers throughout the range of numbers permitted as indexes of lists, vectors, bytevectors, and strings or that result from computing the length of one of these.

On the other hand, above that statement, we can see:

Implementations of Scheme are not required to implement the whole tower of subtypes given in section 6.2.1, but they must implement a coherent subset consistent with both the purposes of the implementation and the spirit of the Scheme language.

This is all what is imposed -- to be logically sound what it's implemented, it does not impose for unlimited arithmetics, as it follows:

It is recommended, but not required, that implementations support exact integers and exact rationals of practically unlimited size and precision,

CodePudding user response:

You have to test it:

#! /bin/bash

size="${1-1MB}"
scm="${2-bigstr_$size.scm}"

if [[ ! -f "$scm" ]]; then
  (
    exec > "$scm"
    printf '(define bigstr "'
    tr -dc '[:alpha:]' < /dev/urandom |
      dd iflag=fullblock status=none bs="$size" count=1
    printf '")\n'
    printf '(display (string-length bigstr))\n'
    printf '(newline)\n'
  )
fi

printf 'String size: %s\n' "$size"

printf 'Petite Chez Scheme %s: ' \
       "$(petite --version 2>&1)"
petite --script "$scm" 2>/dev/null || echo fail

printf 'Chicken Scheme %s: ' \
       "$(csi -release)"
csi -qs "$scm" 2>/dev/null || echo fail

printf 'Guile Scheme %s: ' \
       "$(guile -c '(format #t "~a\n" (version))')"
guile --no-auto-compile "$scm" 2>/dev/null || echo fail

printf 'Gambit Scheme %s: ' \
       "$(gsi -v | sed -n 's/^v\([^ ]*\) .*/\1/p')"
gsi "$scm" 2>/dev/null || echo fail

printf 'MIT/GNU Scheme %s: ' \
       "$(mit-scheme --version | sed -n 's/.*Release \([^ ]*\) .*/\1/p')"
mit-scheme --quiet --load "$scm" </dev/null 2>/dev/null || echo fail

printf 'Racket Scheme %s: ' \
       "$(racket -e '(display (version))')"
racket --script "$scm" 2>/dev/null || echo fail

printf 'Gauche Scheme %s: ' \
       "$(gosh -V | sed -n 's/^(version "\([^"]*\)")/\1/p')"
gosh "$scm" 2>/dev/null || echo fail

On my Debian Schemes work with 10MB but start crashing with 100MB.

String size: 100MB
Petite Chez Scheme 9.5.4: 100000000
Chicken Scheme 5.2.0: fail
Guile Scheme 2.2.7: 100000000
Gambit Scheme 4.9.3: 100000000
MIT/GNU Scheme 10.1.11: ;Aborting!: out of memory
fail
Racket Scheme 7.9: 100000000
Gauche Scheme 0.9.10: 100000000

Be careful with MIT/GNU Scheme, it is extremely slow.

  •  Tags:  
  • Related