2017-04-17 7 views
0

私はSchemeでリストをフォーマットし、各結果の前に行番号でフォーマットされたリストを印刷しようとしています。 ( " - " これは2つのサブリストを持つ大きな "S" リストで、内部の "W")Schemeに行番号を含むリストを印刷する方法は?

(define tree ' 
    ("S" 
    ;;;inside S node 
    (
    ("-" ("A" 3333) ("A" 4444)) 
    ;;;inside W node 
    ("W" 
    (
     ("+" ("R" 0) ("R" 1)) 
     ("+" ("R" 1) ("R" 2)) 
     ("+" ("R" 2) ("R" 3)) 
     ("+" ("R" 3) ("R" 4)) 
     ("+" ("R" 4) ("R" 5)));;;end of adds 
    (("-" ("R" 0) ("R" 1)) 
     ("-" ("R" 1) ("R" 2)) 
     ("-" ("R" 2) ("R" 3)) 
     ("-" ("R" 3) ("R" 4)) 
     ("-" ("A" 1000) ("A" 2000)));;;end of subs 
    );;;end of W node 
    );;;end of S node 
) 
) 

リストを考えます。 そしてここでは私のコードです:

(define (visit a) 
    (bigDumpNode a) 
) 
;;;open the big S list 
(define (bigDumpNode a) 
    (cond 
    ((eq? (car a) "S") 
    (bigSDumpList (cadr a)) 
    ) 
) 
) 
;;;Loop the inner list 
;;;There will be "-" list and "W" list 
(define (bigSDumpList a) 
    (for-each (lambda (x) 
       (intoSmall x) 
)a) 
) 


(define (intoSmall a) 
    ;;;inside W list, we do have more smaller lists 
    (cond 
    ((eq? (car a) "W") 
    (dumpWhile (cdr a)) 
    ) 
    ((or 
     (eq? (car a) "+") 
     (eq? (car a) "-") 
     (eq? (car a) "*") 
     (eq? (car a) "/")) 
     (dumpTwoOperand a) 
    ) 
) 
) 


(define (dumpWhile a) 
    (dumpWhileLoop a) 
) 
;;;loop through the inner W list 
(define (dumpWhileLoop a) 
    (for-each (lambda (x) 
       (operandLoop x) 
       (display "\nbne $00000000") 
      )a) 
) 

(define (operandLoop a) 
    (for-each (lambda (x) 
       (dumpTwoOperand x) 
      )a) 
) 
;;;format the + - */and print add sub mul div console 
(define (dumpTwoOperand a) 
    (cond 
    ((eq? (car a) "+") 
    (display "\nadd") 
    (twoOP(cdr a)) 
    ) 
    ((eq? (car a) "-") 
    (display "\nsub") 
    (twoOP(cdr a)) 
    ) 
    ((eq? (car a) "*") 
    (display "\nmul") 
    (twoOP(cdr a)) 
    ) 
    ((eq? (car a) "/") 
    (display "\ndiv") 
    (twoOP(cdr a)) 
    ) 
    ) 
) 
;;;format A to $ and R is still R 
(define (twoOP a) 
    (oprand1 a) 
    (oprand2 a) 
) 

(define (oprand1 a) 
    (cond 
    ((eq? (caar a) "A") 
    (display " $") 
    (display (cadar a)) 
    ) 
    ((eq? (caar a) "R") 
    (display " R") 
    (display (cadar a)) 
    ) 
    ) 
) 

(define (oprand2 a) 
    (cond 
    ((eq? (caadr a) "A") 
    (display " $") 
    (display (cadadr a)) 
    ) 
    ((eq? (caadr a) "R") 
    (display " R") 
    (display (cadadr a)) 
    ) 
    ) 
) 

私はコンソールで(ツリーを訪問)を入力すると、それは以下の出力になります。

sub $3333 $4444 
add R0 R1 
add R1 R2 
add R2 R3 
add R3 R4 
add R4 R5 
bne $00000000 
sub R0 R1 
sub R1 R2 
sub R2 R3 
sub R3 R4 
sub $1000 $2000 
bne $00000000 

私が欲しいものをするために、各出力の前に行番号を追加することです例:

0: sub $3333,$4444 
1: add R0,R1 
2: add R1,R2 
3: add R2,R3 
4: add R3,R4 
5: add R4,R5 
6: bne $00000000 
... 

どうすればよいですか? ありがとうございます:D

答えて

0

私はこれを行うための最良の方法は、コンソールに直接印刷する代わりに、出力の各行に1つずつ文字列を返すように書式設定コードを書き込むことだと思います。次に、得られた文字列のリストを、行番号と改行を挿入しながら連結する関数に渡すことができます。最後に、その文字列をコンソールに表示します。

(注意:必要に迫られて、私はあなたのコードビットをリファクタリングしました。また、私はfor/list、例えば、いくつかのラケット固有の構文を使用しましたので、あなたは、他の方式のためにそれらの部品を変更する必要があります)

この

(define (visit a) 
    (let* ([line*  (format-node a)] 
     [formatted (format-line* line* 0)]) 
    (display formatted))) 

(define (format-node a) 
    (case (car a) 
     [("S") (append* (map format-group a))])) 

(define (format-group a) 
    (case (car a) 
    [("W")    (append* (map format-while (cdr a)))] 
    [("+" "-" "*" "/") (list (format-two-operand a))])) 

(define (format-while a) 
    (let* ([formatted-op* (map format-two-operand a)]) 
    (append formatted-op* '("bne $00000000")))) 

(define (format-two-operand a) 
    (let* ([operator (car a)] 
     [op-name (case operator 
        [("+") "add"] 
        [("-") "sub"] 
        [("*") "mul"] 
        [("/") "div"])] 
     [operand1 (cadr a)] 
     [operand2 (caddr a)]) 
    (format "~a ~a ~a" op-name (format-operand operand1) (format-operand operand2)))) 

(define (format-operand operand) 
    (let* ([type (car operand)] 
     [value (cadr operand)] 
     [prefix (case type 
        [("A") "$"] 
        [("R") "R"])]) 
    (format "~a~a" prefix value))) 

(define (format-line* line* initial-line#) 
    (let ([formatted-line* (for/list ([line line*] 
            [line# (in-naturals initial-line#)]) 
          (format "~a:\t~a\n" line# line))]) 
    (string-append* formatted-line*))) 
はあなたのサンプルデータのために私に次のような出力が得られます。

0: sub $3333 $4444 
1: add R0 R1 
2: add R1 R2 
3: add R2 R3 
4: add R3 R4 
5: add R4 R5 
6: bne $00000000 
7: sub R0 R1 
8: sub R1 R2 
9: sub R2 R3 
10: sub R3 R4 
11: sub $1000 $2000 
12: bne $00000000 
+0

おかげでたくさん!それはとても甘いですね! –

+0

@zuolizhuよろしくお願いします!あなたが私の答えに満足しているなら、私に感謝する良い方法は、それを受け入れたものとしてマークすることです。 –

関連する問題