Skip to content

Instantly share code, notes, and snippets.

@chorr
Created November 28, 2011 17:16
Show Gist options
  • Save chorr/1401152 to your computer and use it in GitHub Desktop.
Save chorr/1401152 to your computer and use it in GitHub Desktop.
;; 데이터 정의:
(define-struct circle (center radius))
(define-struct square (nw length))
;; 도형(shape)은 다음 두 구조체 중 하나다.
;; 1. a structure: (make-circle p s)
;; p는 posn이고 s는 수다.
;; 2. a structure: (make-square p s)
;; p는 posn이고 s는 수다.
;; 계약, 목적, 헤더:
;; perimeter : shape -> number
;; a-shape의 둘레를 계산함
;; 예: 테스트 참조
;; 템플릿:
;; (define (f a-shape)
;; (cond
;; [(square? a-shape)
;; ... (square-nw a-shape) ... (square-length a-shape) ...]
;; [(circle? a-shape)
;; ... (circle-center a-shape) ... (circle-radius a-shape) ...]))
;; 정의:
(define (perimeter a-shape)
(cond
[(circle? a-shape)
(* (* 2 (circle-radius a-shape)) pi)]
[(square? a-shape)
(* (square-length a-shape) 4)]))
;; 테스트: (예와 동일)
(= (perimeter (make-square ... 3)) 12)
(= (perimeter (make-circle ... 1)) (* 2 pi))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment