Ctrl+P again to print, arrows/tab to navigate results, enter to confirm

    Basic tips

    Many tips from the Common Lisp page also apply to Scheme. Some general differences worth remembering when attempting to port from one to the other:

    Looping

    Looping over a range

    The most generic and powerful iteration construct in Scheme is do:

    ;; Add all numbers between 1 and 10:
    (do ((i   1 (1+ i))
         (sum 0 (+ sum i)))
      ((> i 10) sum) ; if i>10: break and return sum (55)
    
      (display sum)
      (newline))
    

    See the Common Lisp page or the Chez Scheme User Guide for more details and use cases.

    If looping from 0 to n-1, you might consider (iota n) or (enumerate ls) (equivalent to (iota (length ls))) in conjunction with the list iteration strategies below.

    Looping over a list

    There are several ways one can loop over a list, perform some operation, and output the results. Here are a few ways one could print all the arguments to a hole in uppercase:

    (do((a(cdr(command-line))(cdr a)))((null? a))(printf"~a~n"(string-upcase(car a))))
    (for-each(lambda(a)(printf"~a~n"(string-upcase a)))(cdr(command-line)))
    (printf"~{~a~n~}"(map(lambda(a)(string-upcase a))(cdr(command-line))))
    (andmap(lambda(a)(printf"~a~n"(string-upcase a)))(cdr(command-line)))
    (memp(lambda(a)(printf"~a~n"(string-upcase a))#f)(cdr(command-line)))
    (printf"~{~a~n~}"(map string-upcase(cdr(command-line))))
    (printf"~:@(~{~a~n~}~)"(cdr(command-line)))
    

    Note that andmap and memp above cannot be replaced by map, as map does not necessarily iterate over its arguments in order.

    Splitting a string

    The shortest way to split a string on spaces:

    (read(open-input-string(format"(~a)"x)))
    

    This yields a list of numbers and symbols, the latter of which can be converted to strings via symbol->string. If you process the split items in-order, you may instead make use of repeated read calls (without format). If you need to split on something besides spaces, try using memq.

    Format strings

    printf and format use Lisp format directives, which are very powerful for golfing.

    ; Scheme
    (printf "string")
    (format "string")
    (format condition "string")
    
    ; Lisp
    (format t "string")
    (format nil "string")
    (format condition "string")
    

    However, there are some minor differences between Common Lisp and Scheme's implementations:

    2:1 Packer (written in Ruby)

    print '(eval(read(open-input-string(utf8->string(string->utf16"',
    "(display 10)".encode('utf-8', 'utf-16be'),
    '")))))'
    

    Reference