[racket] else: not allowed as an expression in: else

From: Niitsuma Hirotaka (hirotaka.niitsuma at gmail.com)
Date: Tue Dec 21 18:57:58 EST 2010

I try to run this code on racket

http://www.daniweb.com/forums/thread277930.html

After little modify I enables run this code on swindle.
But when I chose language racket, this code cause error

else: not allowed as an expression in: else

flowing is the modified code
-------------------------------------------

#lang racket

(require eopl/eopl)

;The helpers used in parse and unparse

;(define list-of
;  (lambda (pred)
;    (lambda (val)
;      (or (null? val)
;	  (and (pair? val)
;	       (pred (car val))
;	       ((list-of pred) (cdr val)))))))

;combines 2 lists of the same size
;as a list of list with corresponding elements
(define combine
  (lambda (ls1 ls2)
    (if (null? ls1)
        '()
	(append (list (list (car ls1) (car ls2)))
	      (combine (cdr ls1) (cdr ls2))))))


(define improperlist?
  (lambda (x)
    (and (pair? x) (not (list? x)))))
		
(define literal?
  (lambda (x)
    (or (number? x)
	(string? x)
	(boolean? x)
	(vector? x))))

(define list-of-lists?
  (lambda (x)
    (and (list? x)
	 (andmap list? x))))

(define lengths-equal-two?
  (lambda (x)
    (= (length x) 2)))

(define firsts
  (lambda (ls)
    (if (null? ls)
        '()
	(cons (caar ls)
	      (firsts (cdr ls))))))

(define lasts
  (lambda (ls)
    (if (null? ls)
        '()
	(cons (cadar ls)
	      (lasts (cdr ls))))))




(define-datatype expression expression?
  [var-exp
    (id symbol?)]
  [lambda-exp
    (vars (list-of symbol?))
    (bodies (list-of expression?))]
  [lambda-var-improp-exp
    (vars (list-of improperlist?))
    (bodies (list-of expression?))]
  [lambda-var-sym-exp
    (vars symbol?)
    (bodies (list-of expression?))]
  [set-exp
    (id symbol?)
    (var expression?)]
  [app-exp
    (rands (list-of expression?))]
  [lit-exp
    (id literal?)]
  [if-exp
    (test expression?)
    (true expression?)
    (false expression?)]
  [if-noelse-exp
    (test expression?)
    (true expression?)]
  [let-exp
    (vars (list-of symbol?))
    (vals (list-of expression?))
    (bodies (list-of expression?))]
  [let*-exp
    (vars (list-of symbol?))
    (vals (list-of expression?))
    (bodies (list-of expression?))]
  [letrec-exp
    (vars (list-of symbol?))
    (vals (list-of expression?))
    (bodies (list-of expression?))]
  [letname-exp
    (name symbol?)
    (vars (list-of symbol?))
    (vals (list-of expression?))
    (bodies (list-of expression?))])





;parse
(define parse-exp
  (lambda (datum)
    (cond
      [(symbol? datum) (var-exp datum)]
      [(literal? datum) (lit-exp datum)]
      [(pair? datum)
       (cond
	 [(eqv? (car datum) 'lambda)
	  (if (< (length (cdr datum)) 2)
	      (eopl:error 'parse-exp "Invalid lambda syntax ~s" datum)
	      (cond
	        [(list? (cadr datum))
	         (lambda-exp (cadr datum)
			     (map parse-exp (cddr datum)))]
	        [(symbol? (cadr datum))
	         (lambda-var-sym-exp (cadr datum)
				     (map parse-exp (cddr datum)))]
	        [(improperlist? (cadr datum))
	         (lambda-var-improp-exp (cadr datum)
				        (map parse-exp (cddr datum)))]
	        [else
	          (eopl:error 'parse-exp
	 	    "Invalid lambda syntax ~s" datum)]))]
	 [(eqv? (car datum) 'set!)
	  (if (= (length (cdr datum)) 2)
	      (set-exp (cadr datum)
		       (parse-exp (caddr datum)))
	      (eopl:error 'parse-exp
		"Invalid set! syntax ~s" datum))]	
	 [(eqv? (car datum) 'set)
		(eopl:error "Invalid set syntax ~s" datum)]
	 [(eqv? (car datum) 'if)
	  (cond
	    [(= (length (cdr datum)) 3)
	     (if-exp (parse-exp (cadr datum))
		     (parse-exp (caddr datum))
		     (parse-exp (cadddr datum)))]
	    [(= (length (cdr datum)) 2)
	     (if-noelse-exp (parse-exp (cadr datum))
			    (parse-exp (caddr datum)))]
	    [else
	      (eopl:error 'parse-exp
		"Invalid if syntax ~s" datum)])]
	 [(and (eqv? (car datum) 'let) (list? (cadr datum)))
	  (cond
	    [(< (length (cdr datum)) 2)
	     (eopl:error 'parse-exp "Invalid let syntax ~s" datum)]
	    [(and (list-of-lists? (cadr datum))
		   (andmap lengths-equal-two? (cadr datum)))
	      (let-exp (firsts (cadr datum))
		       (map parse-exp (lasts (cadr datum)))
		       (map parse-exp (cddr datum)))]
	    [else
	      (eopl:error 'parse-exp
		"Invalid let syntax ~s" datum)])]
	 [(and (eqv? (car datum) 'let) (symbol? (cadr datum)))
	  (cond
	    [(< (length (cdr datum)) 3)
	     (eopl:error 'parse-exp "Invalid named let syntax ~s" datum)]
	    [(and (list-of-lists? (caddr datum))
		   (andmap lengths-equal-two? (caddr datum)))
	      (letname-exp (cadr datum)
			   (firsts (caddr datum))
		           (map parse-exp (lasts (caddr datum)))
		           (map parse-exp (cdddr datum)))]
	    [else
	      (eopl:error 'parse-exp
		"Invalid named let syntax ~s" datum)])]
	 [(eqv? (car datum) 'let*)
	  (cond
	    [(< (length (cdr datum)) 2)
	     (eopl:error 'parse-exp "Invalid let* syntax ~s" datum)]
	    [(and (list-of-lists? (cadr datum))
		   (andmap lengths-equal-two? (cadr datum)))
	      (let*-exp (firsts (cadr datum))
		       (map parse-exp (lasts (cadr datum)))
		       (map parse-exp (cddr datum)))]
	    [else
	      (eopl:error 'parse-exp
		"Invalid let* syntax ~s" datum)])]
	 [(eqv? (car datum) 'letrec)
	  (cond
	    [(< (length (cdr datum)) 2)
	     (eopl:error "Invalid letrec syntax ~s" datum)]
	    [(and (list-of-lists? (cadr datum))
		   (andmap lengths-equal-two? (cadr datum)))
	      (letrec-exp (firsts (cadr datum))
		       (map parse-exp (lasts (cadr datum)))
		       (map parse-exp (cddr datum)))]
	    [else
	      (eopl:error 'parse-exp
		"Invalid letrec syntax ~s" datum)])]
	 [(list? datum)
	       (app-exp (map parse-exp datum))]
	 [else
	   (eopl:error 'parse-exp "Invalid syntax ~s" datum)])]
      [else (eopl:error 'parse-exp
              "Invalid syntax ~s" datum)])))



;unparse
(define unparse-exp
  (lambda (exp)
    (cases expression exp
      [var-exp (id) id]
      [lit-exp (id) id]
      [lambda-exp (vars bodies)
        (append (list 'lambda vars)
          (map unparse-exp bodies))]
      [lambda-var-sym-exp (vars bodies)
	(append (list 'lambda vars)
		(map unparse-exp bodies))]
      [lambda-var-improp-exp (vars bodies)
	(append (list 'lambda vars)
		(map unparse-exp bodies))]
      [set-exp (id var)
	(list id (unparse-exp var))]
      [app-exp (rands)
	(map unparse-exp rands)]
      [if-exp (test true false)
	(list 'if (unparse-exp test)
	          (unparse-exp true)
		  (unparse-exp false))]
      [if-noelse-exp (test true)
	(list 'if (unparse-exp test) (unparse-exp true))]
      [let-exp (vars vals bodies)
	(append (list 'let (combine vars vals))
	        (map unparse-exp bodies))]
      [let*-exp (vars vals bodies)
	(append (list 'let* (combine vars vals))
		(map unparse-exp bodies))]
      [letrec-exp (vars vals bodies)
	(append (list 'letrec (combine vars vals))
		(map unparse-exp bodies))]
      [letname-exp (name vars vals bodies)
	(append (list 'let name (combine vars vals))
		(map unparse-exp bodies))])))


Posted on the users mailing list.