I have a scheme program which is executed using the command ; (primes<= n) to give me all the primes less than n

For example, ; (primes<= 200) gives me all the primes less than 200

How do I create an executable in Linux for the program below taking n as an argument

---------Scheme Program------------------------------------------------

    #lang racket

(define  (interval-list m n)
  (if (> m n)
      (cons m (interval-list (+ 1 m) n))))
(define (sieve l)
  (define (remove-multiples n l)
    (if (null? l)
         (if  (= (modulo (car l) n) 0)      ; division test
              (remove-multiples n (cdr l))
              (cons (car l)
                    (remove-multiples n (cdr l))))))
  (if (null? l)
      (cons (car l)
             (sieve (remove-multiples (car l) (cdr l))))))
(define (primes<= n)
  (sieve (interval-list 2 n)))

The above program is executed as (primes<= 100)prints all the primes less than 100


I believe you'll want to check your Scheme system's documentation for:

  • compilation to native executable, and
  • passing command line arguments from the OS shell

For example, if you were using MIT/GNU Scheme, I'd refer you to:

If you have flexibility over which Scheme interpreter you use, MIT/GNU Scheme is ported to many operating systems, including Linux, so would work well for the example above.

| improve this answer | |

In order to turn this Scheme program into a file that runs a Scheme interpreter, start it with a shebang line, and make it executable (chmod +x /path/to/program). A shebang line is the first line of an executable file and starts with #! followed by the full path to the interpreter. Since you're using Racket:


If you don't want to specify the full path to the interpreter, but want instead to have it looked up in the executable search path $PATH, then use

#!/usr/bin/env racket

To access the command line arguments, Racket provides a command line parsing library](http://docs.racket-lang.org/reference/Command-Line_Parsing.html), or you can retrieve the command line parameters directly with (current-command-line-arguments). You'll need to invoke racket with the -f option:

#!/usr/bin/racket -f
(define arg (string->number (vector-ref (current-command-line-arguments) 0)))
(display (primes<= arg))

This cannot be combined with #!/usr/bin/env to look up racket in $PATH. If you want that, make your script a polyglot:

":"; exec racket -f "$0" "$@"

If you want your script to be portable across Scheme implementation, this is possible provided that the implementation follows SRFI 22 (which Racket doesn't).

#!/usr/bin/env scheme-r5rs
(define (main argv)
  (display (primes<= (string->number (list-ref argv 1))))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.