谢尔宾斯基地毯方案代码翻译

发布于 2024-12-23 05:39:12 字数 219 浏览 3 评论 0原文

我在 http://rosettacode.org/wiki/Sierpinski_carpet#Scheme 找到了生成 Sierpinski 地毯的代码- 但它不会在 DrRacket 环境或 WeScheme 中运行。有人可以为这两种环境提供解决方案吗?

I found code for generating Sierpinski carpet at http://rosettacode.org/wiki/Sierpinski_carpet#Scheme - but it won't run in the DrRacket environment or WeScheme. Could someone provide solutions for either environments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵强ㄟ 2024-12-30 05:39:12

看起来这段代码在 DrRacket 中运行良好,在前面添加一行

#lang racket

表明该代码是在 Racket 中编写的。如果这还不够,我可以提供更多详细信息。

It looks like this code runs fine in DrRacket after prepending a

#lang racket

line indicating that the code is written in Racket. I can provide more detail if this is not sufficient.

旧瑾黎汐 2024-12-30 05:39:12

我已经将程序翻译为在 WeScheme 下运行。我做了一些更改:我没有使用(显示)和(换行),而是使用 WeScheme 提供的图像基元来制作稍微更好的图片。您可以查看正在运行的程序及其源代码。为了方便起见,我还在这里附上了来源:

;; 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)

I've translated the program to run under WeScheme. I've made a few changes: rather than use (display) and (newline), I use the image primitives that WeScheme provides to make a slightly nicer picture. You can view the running program and its source code. For convenience, I also include the source here:

;; 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)
溺孤伤于心 2024-12-30 05:39:12

这是 WeScheme 的修改后的代码。 WeScheme 不支持 do-loop 语法,因此我使用 srfi-1 中的 Expand 代替

(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))))

Here is the modified code for WeScheme. WeScheme don't support do-loop syntax, so I use unfold from srfi-1 instead

(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))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文