2010-03-29 60 views
1

我一直在为学校开发一个项目,该项目从一个类文件中获取函数并将其转换为对象/类。这个任务都是关于方案中的面向对象编程。计划格式帮助

但是我的问题是我的代码格式不正确。

无论何时我给它一个文件传递给列表中的类的方法,它都会给我提供的输出,使得类永远不会被声明。我不能为我的生活弄清楚如何让圆括号包装方法列表来删除。

我真的很感激任何帮助。

下面是输出,类文件和代码。

(define pointInstance 
    (let ((myx 1) (myy 2)) 
    (lambda msg 
     (cond 
     (((eq? (car msg) getx) myx) 
     ((eq? (car msg) gety) myy) 
     ((eq? (car msg) setx) (set! myx x)) 
     ((eq? (car msg) show) (begin (display "[") (display myx) (display ",") (display myy) (display "]")))))))) 

如果你看一下刚刚COND后,你会看到所有的EQ语句是如何包含在列表中。除非他们没有被顶层列表所包裹,否则我无法让它正常工作。

;;;; PART1 --- A super-easy set of classes. Just models points and lines. Tests all of >the 
;; basics of class behavior without touching on anything particularly complex. 

(class pointInstance (parent:) (constructor_args:) 
    (ivars: (myx 1) (myy 2)) 
    (methods: 
    (getx() myx) 
    (gety() myy) 
    (setx (x) (set! myx x)) 
    (show() (begin (display "[") (display myx) (display ",") (display myy) (display "]"))) 
    )) 



(require (lib "trace.ss")) 

;; Continue reading until you hit the end of the file, all the while 
;; building a list with the contents 
(define load-file 
(lambda (port) 
(let ((rec (read port))) 
(if (eof-object? rec) 
'() 
(cons rec (load-file port)))))) 

;; Open a port based on a file name using open-input-file 
(define (load fname) 
(let ((fport (open-input-file fname))) 
(load-file fport))) 



;(define lis (load "C:\\Users\\Logan\\Desktop\\simpletest.txt")) 
;(define lis (load "C:\\Users\\Logan\\Desktop\\complextest.txt")) 
(define lis (load "C:\\Users\\Logan\\Desktop\\pointinstance.txt")) 

;(display (cdaddr (cdddar lis))) 

(define makeMethodList 
    (lambda (listToMake retList) 
    ;(display listToMake) 
    (cond 
     [(null? listToMake) 
     retList 
     ;(display "The list passed in to parse was null") 
     ] 
     [else 
     (makeMethodList (cdr listToMake) (append retList (list (getMethodLine   listToMake)))) 
     ] 
     ) 
    )) 
;(trace makeMethodList) 

;this works provided you just pass in the function line 
(define getMethodLine 
    (lambda (functionList) 
    `((eq? (car msg) ,(caar functionList)) ,(caddar functionList)))) 

(define load-classes 
    (lambda paramList 
    (cond 
    [(null? paramList) (display "Your parameters are null, man.")] 
[(null? (car paramList))(display "Done creating class definitions.")] 
[(not (null? (car paramList))) 

    (begin 
    (let* ((className (cadaar paramList)) 
      (classInstanceVars (cdaddr (cddaar paramList))) 
      (classMethodList (cdr (cadddr (cddaar paramList)))) 
      (desiredMethodList (makeMethodList classMethodList '())) 

      ) 
     ;(display "Classname: ") 
     ;(display className) 
     ;(newline)(newline) 

     ;(display "Class Instance Vars: ") 
     ;(display classInstanceVars) 
     ;(newline)(newline) 

     ;(display "Class Method List: ") 
     ;(display classMethodList) 
     ;(newline) 

     ;(display "Desired Method List: ") 
     ;(display desiredMethodList)) 
     ;(newline)(newline) 

;---------------------------------------------------- 
;do not delete the below code!` 
     `(define ,className 
     (let ,classInstanceVars 
      (lambda msg 
      ;return the function list here 
      (cond ,(makeMethodList classMethodList '()))) 
      )) 
;--------------------------------------------------- 
))] 
) 
)) 

(load-classes lis) 
;(load-classes lis) 
;(load-classes-helper lis) 
;(load-classes "simpletest.txt") 
;(load-classes "complextest.txt") 

;method list 
;(display (cdr (cadddr (cddaar <class>)))) 

回答

1

cond的第1个子句中有太多左括号。

IE:

(((eq? (car msg) getx) myx) 
^ 

更新时间:

你在找这个?

(cond ,@(makeMethodList classMethodList '()) 
     ^^ 

或者,你可以这样做:

(cond . ,(makeMethodList classMethodList '()) 
+0

我知道,我不知道如何摆脱它。对于如何摆脱方法的外部括号,你有什么建议吗? – Logan 2010-03-29 05:27:40

+1

@Logan:更新了答案。 – leppie 2010-03-29 06:12:48

+0

@John:请不要将问题标记为家庭作业,除非OP声明它确实是家庭作业,或者除非您有水晶球。 – 2010-03-29 19:25:36