1
:算術式をフラット化するためのSchemeプロシージャを書くには?たとえば
(* (* (* 1 2) 3) (* 4 5)) = (* 1 (* 2 (* 3 (* 4 5))))
私は今、時間のカップルのためにこの手順を記述する方法を把握しようとして座ってきたが、私はそれが仕事を得るように見えることはできません。
たプログラムが(ただし、それは、期待どおりに動作しません)です:
(define interpret-arithmetic-expression_Magritte_bizarre
(lambda (e)
(cond
[(is-literal? e)
(make-literal (literal-1 e))]
[(is-plus? e)
(if (is-plus? (plus-1 e))
(make-plus (interpret-arithmetic-expression_Magritte_bizarre (plus-1 (plus-1 e)))
(make-plus (interpret-arithmetic-expression_Magritte_bizarre (plus-2 (plus-1 e)))
(interpret-arithmetic-expression_Magritte_bizarre (plus-2 e))))
(make-plus (interpret-arithmetic-expression_Magritte_bizarre (plus-1 e))
(interpret-arithmetic-expression_Magritte_bizarre (plus-2 e))))]
[(is-times? e)
(if (is-times? (times-1 e))
(make-times (interpret-arithmetic-expression_Magritte_bizarre (times-1 (times-1 e)))
(make-times (interpret-arithmetic-expression_Magritte_bizarre (times-2 (times-1 e)))
(interpret-arithmetic-expression_Magritte_bizarre (times-2 e))))
(make-times (interpret-arithmetic-expression_Magritte_bizarre (times-1 e))
(interpret-arithmetic-expression_Magritte_bizarre (times-2 e))))]
[else
(errorf 'interpret-arithmetic-expression_Magritte
"unrecognized expression: ~s"
e)])))
問題の詳細な仕様はありますか? +演算子の例ですか? – coredump
どうして '(* 1 2 3 4 5)'にならないのですか? – Sylwester
プラス演算子は同じ問題です。すなわち、(+(+ 4 5)(+ 1 9))=(+ 4(+ 1))) – user111854