ctarbi.de - scsh·examples

Snippets

hello1:

(display "hello world!\n")

hello2:

(define (main args)
    (display "hello world!\n")
    (display (string-append
        "called with "
        (number->string (length args))
        " args\n"))
    (format #t "args: ~s~%" args))

args1:

(define argc (length command-line-arguments))
(format #t "argc: ~a~%" argc)

args2:

(define argc (length command-line-arguments))
(format #t "argc: ~a~%" argc)
(format #t "args: ~s~%" command-line-arguments)
(format #t "argv 0: ~a~%" (argv 0))

args3:

(define argc (length command-line-arguments))
(define (show v)
    (display v)
    (newline))
(define (show-args n)
    (define (loop i)
        (if (<= i n)
            (begin
                (format #t "argv ~a: ~s~%" i (argv i))
                (loop (+ i 1)))
            (values)))
    (loop 1))
(show argc)
(show (argv 0))
(show-args argc)

for-each1:

(define argc (length command-line-arguments))
(format #t "argc: ~a~%" argc)
(format #t "args: ~s~%" command-line-arguments)
(for-each (lambda (arg)
    (format #t "arg: ~s~%" arg))
    command-line-arguments)

fac1:

(define !
    (lambda (n)
        (if (= n 0)
            1
            (* n (! (- n 1))))))
(format #t "~s~%" (! 5))

fac2:

(define (! n)
    (if (= n 0)
        1
        (* n (! (- n 1)))))
(format #t "~s~%" (! 5))

fac3:

(define (! n)
    (define (!! acc n)
        (if (= n 1)
            acc
            (!! (* n acc) (- n 1))))
    (!! 1 n))
(format #t "~s~%" (! 5))

list-path:

(define (writeln x) (display x) (newline))
(for-each writeln ((infix-splitter ":") (getenv "PATH")))

list-executables:

(define (executables dir)
  (with-cwd dir
    (filter file-executable? (directory-files dir #t))))
(define (writeln x) (display x) (newline))
(for-each writeln
  (append-map executables ((infix-splitter ":") (getenv "PATH"))))

stdin1:

(let ((input (read-line (current-input-port))))
  (for-each (lambda (x) (display x) (newline))
            (string-tokenize input)))

Live Literate Program

All scripts follow the live literate program technique, some examples:

hello1:

#!/bin/sh
# https://ctarbide.github.io/pages/2024/2024-02-05_12h00m11_hello-worlds/
# https://github.com/ctarbide/coolscripts/blob/master/bin/nofake-exec.nw
set -eu; set -- "${0}" --ba-- "$@" --ea--
exec nofake-exec.sh --error -Rprog "$@" -- scsh -s
exit 1

This is a live literate program.

<<prog>>=
(display "hello world!\n")
@

hello2:

#!/bin/sh
# https://ctarbide.github.io/pages/2024/2024-02-05_12h00m11_hello-worlds/
# https://github.com/ctarbide/coolscripts/blob/master/bin/nofake-exec.nw
set -eu; set -- "${0}" --ba-- "$@" --ea--
exec nofake-exec.sh --error -Rprog "$@" -- scsh -e main -s
exit 1

This is a live literate program.

<<prog>>=
(define (main args)
    (display "hello world!\n")
    (display (string-append
        "called with "
        (number->string (length args))
        " args\n"))
    (format #t "args: ~s~%" args))
@

nofake-exec.sh is provided by coolscripts.

References

More details in the link below.

This page was last modified on February 16, 2024 at 18:07:46 UTC.