- Blog -

(A cheesy homepage for Justin Collins)
Updates to Schme2Lisp

Alright, first of all, a word of caution. A big word of caution: Since LISP does not have a false value, but uses NIL or (), those are considered to be false in LISP. In Scheme, however, () is NOT FALSE (but true). So when this little script converts from #f to NIL, be careful.

I only say this because I just spent a bunch of time trying to figure out where the translation went wrong, because I was relying on () being true.

Another thing to look out for is the list-ref -> nth conversion, which may not always be correct. I can’t remember.

Changes to Scheme2Lisp:

  • Very important: equal? -> equal (not eql)
  • Added: (define (bob)) -> (defun bob ())
  • Added: (list-ref) -> (nth)

Nicer version of my Scheme to LISP converter
This is really super simple, but it lets me use Dr. Scheme for my AI class, so it does what I need it to do.

#!/usr/bin/env ruby
#
#Scheme2Lisp - Basic conversion between Scheme and LISP syntax
#
#Author: Justin Collins
#
#Usage: ./scheme2lisp.rb <schemefile>
#
#or
#    ./scheme2lisp.rb <schemefile> > <lispfile>
#
#Output: LISP (hopefully) to standard output
#
#Version: 0.0.5

if ARGV[0]
    f = File.open(ARGV[0])
else
    f = $stdin
end

conversion = {
        "[" => "(",
        "]" => ")",
        "else" => "t",
        "#t" => "t",
        "#f" => "nil",
        "list?" => "listp",
        "symbol?" => "symbolp",
        "equal?" => "eql",
        "null?" => "null",
        "even?" => "evenp",
        "odd?" => "oddp",
        "list?" => "listp",
        "ceiling" => "ceil",
        "display" => "print",
        "(newline)" => "",
        "modulo" => "mod",
        "number?" => "atom" 
}

f.readlines.each do |line|
    line = line.gsub(Regexp.union(*conversion.keys)) { |match|
            conversion[match]
    }.gsub(/\(define \(([^ ]*) (.*?)\)/, '(defun \1 (\2)').gsub(/\(define (.*?) (.*?)\)/, '(defconstant \1 \2)')
    puts line if line
end