Re: [請益] 沒有想像中簡單的小問題

看板Programming作者 (喲)時間16年前 (2009/03/24 10:08), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串16/34 (看更多)
※ 引述《yauhh (喲)》之銘言: : ANSI Common-Lisp 版本: 再修改一下自己的程式. 在貼程式之前,先談這一題,看了前面一連串的程式碼, 覺得其中在思考構面和程式的規格大不大,有相當微妙的優缺點. 像原po的程式,就是寫多少程式做多少事情; 先印上半段,再印下半段. 但通常解決這種問題,我們比較希望抓住關鍵的參數,然後能決定輸出多大的菱形. 我將自己的Lisp程式改一下,覺得比較好: 要輸入 (main 3 5), 3 是上尖端數字, 5 是菱形高度. 於是它要印出尖端是 3, 高度是 5 的菱形. 所以程式如下: (defun main (n h) (diamond (list (repeat-char #\space (/ (- h 1) 2)) n))) (defun diamond (struct) (cond ((null (car struct)) (print* (reverse (cdr struct))) (terpri)) (t (print* struct) (terpri) (setf ta (car struct)) (setf td (cdr struct)) (pop ta) (setf tx (pop td)) (push tx td) (push (+ tx 1) td) (push (+ tx 2) td) (diamond (cons ta td)) (print* struct) (terpri)))) (defun print* (list) (cond ((null (car list)) nil) ((listp (car list)) (print* (car list)) (print* (reverse (cdr list)))) (t (princ (car list)) (print* (cdr list))))) (defun repeat-char (c n) (cond ((< n 1) nil) (t (cons c (repeat-char c (- n 1)))))) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.112.224.82
文章代碼(AID): #19o424m- (Programming)
討論串 (同標題文章)
文章代碼(AID): #19o424m- (Programming)