Arc Forumnew | comments | leaders | submitlogin
Can Arc run as a script instead of REPL?
15 points by kens 6127 days ago | 13 comments
How can I run Arc as a script instead of a REPL? For instance, suppose I want to use an Arc script in a Unix pipeline or cron job. To make this concrete, suppose I want a trivial script:

  $ ls | arc-word-count-script
  42
or even more trivially:

  $ arc-hello-world-script
  hello world
What magic needs to go in arc-hello-world-script? The following is close, but outputs a couple lines of junk:

  echo '"hello world"(quit)' | mzscheme -m -f as.scm


22 points by vincenz 6127 days ago | link

Yes, quite easily, just make a new script like this:

ar.sh:

    #! /bin/sh
    #|
    exec mzscheme -fmv "$0" ${1+"$@"}
    |#
    (require mzscheme)
    (load "ac.scm") 
    (require "brackets.scm")
    (use-bracket-readtable)
    (aload "arc.arc")
    (aload "libs.arc")
    (aload (car (vector->list (current-command-line-arguments))))
main.arc:

    (whilet line (readline)
      (prn line))
shell:

    chmod +x ar.sh
    ar.sh main.arc

-----

2 points by kens 6127 days ago | link

Thank you! That's hugely helpful! I have to say, though, that 11 lines of boilerplate kind of obliterates Arc's conciseness advantage in the Arc Challenge :-)

-----

2 points by jfm3 6127 days ago | link

Not really, the Arc Challenge specifically discounts code for setting up libraries. All the script is doing is setting up libraries and loading your code.

-----

1 point by shader 5750 days ago | link

Is there a way to execute a script that a) runs some arc or scheme code (specifically "(thread (asv))" or similar) and then b) starts the repl? Do I just change your main.arc to run the arc code I want, and then call ($ (tl))?

-----

1 point by laughingboy 6124 days ago | link

This works great, but what exactly does `"$0" ${1+"$@"}` mean? I'm not a keen shell scripter.

-----

4 points by bayareaguy 6124 days ago | link

This is the common sh shell idiom to properly pass the original command line to the program being invoked.

"$0" expands to the program name ("ar.sh" in his example).

${1+"$@"} is conditional:

if $1 (the first positional parameter) is unset it expands to nothing.

however if $1 is set, it expands to "$@", which in turn expands to all the parameters, each one quoted as a separate word.

-----

1 point by laughingboy 6124 days ago | link

Thank you.

-----

1 point by byronsalty 6109 days ago | link

I'm wondering about this #| .. |# syntax. Never seen that before.

-----

2 points by cadaver 6109 days ago | link

It's the scheme way to write multiline comments (possibly other lisps?). It's specified in the R6RS.

-----

1 point by kennytilton 6109 days ago | link

Also Common Lisp, and this reminds me that this is something I missed when working on some Arc code.

-----

2 points by cadaver 6109 days ago | link

#| ... |# works in Arc too. Though whether by design or because mzscheme supports it, I don't know.

-----

1 point by kennytilton 6109 days ago | link

Doh! :)

-----

1 point by nex3 6127 days ago | link

At the moment, I don't believe this is possible.

-----