2011-12-30 4 views

답변

2

WeScheme에서 실행되도록 프로그램을 번역했습니다. 몇 가지 변경을했습니다 : (디스플레이)와 (newline)을 사용하기보다는, WeScheme이 제공하는 이미지 프리미티브를 사용하여 약간 더 멋진 그림을 만듭니다. 당신은 view the running program and its source code 일 수 있습니다. 편의를 위해 여기에 소스를 포함합니다.

;; Sierpenski carpet. 
;; http://rosettacode.org/wiki/Sierpinski_carpet#Scheme 

(define SQUARE (square 10 "solid" "red")) 
(define SPACE (square 10 "solid" "white")) 

(define (carpet n) 
    (local [(define (in-carpet? x y) 
      (cond ((or (zero? x) (zero? y)) 
        #t) 
       ((and (= 1 (remainder x 3)) (= 1 (remainder y 3))) 
        #f) 
       (else 
        (in-carpet? (quotient x 3) (quotient y 3)))))] 

    (letrec ([outer (lambda (i) 
        (cond 
         [(< i (expt 3 n))      
         (local ([define a-row 
           (letrec ([inner 
              (lambda (j) 
              (cond [(< j (expt 3 n)) 
                (cons (if (in-carpet? i j) 
                   SQUARE 
                   SPACE) 
                  (inner (add1 j)))] 
                [else 
                empty]))]) 
            (inner 0))]) 
         (cons (apply beside a-row) 
           (outer (add1 i))))] 
         [else 
         empty]))]) 
    (apply above (outer 0))))) 


(carpet 3) 
+0

정말 고마워요. 코드는 매우 읽기 쉽습니다. 안부, – lifebalance

1

다음은 WeScheme의 수정 된 코드입니다. WeScheme가 할 루프 구문을 지원하지 않는, 그래서 srfi-1에서 펼쳐 대신 사용

그것은이 코드처럼 보이는
(define (unfold p f g seed) 
    (if (p seed) '() 
    (cons (f seed) 
      (unfold p f g (g seed))))) 

(define (1- n) (- n 1)) 

(define (carpet n) 
    (letrec ((in-carpet? 
      (lambda (x y) 
       (cond ((or (zero? x) (zero? y)) 
         #t) 
        ((and (= 1 (remainder x 3)) (= 1 (remainder y 3))) 
         #f) 
        (else 
         (in-carpet? (quotient x 3) (quotient y 3))))))) 
    (let ((result 
      (unfold negative? 
        (lambda (i) 
         (unfold negative? 
           (lambda (j) (in-carpet? i j)) 
           1- 
           (1- (expt 3 n)))) 
        1- 
        (1- (expt 3 n))))) 
     (for-each (lambda (line) 
         (begin 
          (for-each (lambda (char) (display (if char #\# #\space))) line) 
          (newline))) 
       result)))) 
+0

이렇게하면 원하는 그래픽 그림이 생성되지 않은 것처럼 보입니다. – lifebalance

+0

[출력] (http://www.wescheme.org/view?publicId=bathe-pesky-cover-lunar-clock) 여기를 볼 수 있습니다. 문제가있는 것 같다 (lambda (char) (display (char # \ # # \ space)). (for-each (lambda (char) (display (char "#" ") 그리고 그것을 작동했습니다. 그러나 글꼴이 고정되어 있지 않기 때문에 디스플레이가 들쭉날쭉하게 나타났습니다 .Wescheme"디스플레이 "기능에서 고정 된 글꼴을 사용하는 방법을 알아낼 수 없습니다. – lifebalance

4

코드임을 나타내는

#lang racket 

라인을 붙이는 후 DrRacket에서 잘 실행 라켓에 쓰여 있습니다. 이것이 충분하지 않다면 더 자세한 정보를 제공 할 수 있습니다.