무냐의 개발일지

[OSSU] <UBCx HtC1x_How to Code> / 2_How to Design Data 본문

OSSU_CS coursework

[OSSU] <UBCx HtC1x_How to Code> / 2_How to Design Data

무냐코드 2024. 4. 24. 22:51

| Cond expressions

2개 이상의 조건이 있을 때 

cond [(조건) (답)]

 

 

 

(require 2htdp/image)

(define I1 (rectangle 10 20 "solid" "red"))
(define I2 (rectangle 20 20 "solid" "red"))
(define I3 (rectangle 20 10 "solid" "red"))

(define (aspect-ratio img)
  (cond [(> (image-height img) (image-width img)) "tall"]
        [(= (image-height img) (image-width img)) "square"]
        [else "wide"]))

(aspect-ratio I1)
(aspect-ratio I2)
(aspect-ratio I3)

 

 

| Data Definitions (참고)

입력값을 특정 값으로만 한정지어주는 것 

그리고 function은 그 입력값 내의 값만 받아서 돌아가도록 한다

 

Data Design 하는 스텝 5가지

The first step of the recipe is to identify the inherent structure of the information.

Once that is done, a data definition consists of four or five elements:
  1. A possible structure definition (not until compound data)
  2. A type comment that defines a new type name and describes how to form data of that type.
  3. An interpretation that describes the correspondence between information and data.
  4. One or more examples of the data.
  5. A template for a 1 argument function operating on data of this type.
In the first weeks of the course we also ask you to include a list of the template rules used to form the template.

 

💡 Python (https://moonyacode.tistory.com/82) 에서
Assertation이 있었지! function 맨 앞에 인풋 조건을 설정해주는 거
assert not len(gredes)==0, 'no grades data'
이런 식이었다

 

 

- Data Driven Templates (레시피 링크!)

(define (fn-for-type-name x)
  <body>)

 

 

| Data Form

 

| Enumeration

정해진 몇 개 중 하나의 옵션을 쓸 때, one of 을 쓸거다

Use an enumeration when the information to be represented consists of a fixed number of distinct items, such as colors, letter grades etc. 

 

Functions operating on enumerations should have (at least) as many tests as there are cases in the enumeration.

;; LightState is one of:
;;  - "red"
;;  - "yellow"
;;  - "green"
;; interp. the color of a traffic light

;; <examples are redundant for enumerations>
 
#;
(define (fn-for-light-state ls)
  (cond [(string=? "red" ls) (...)]
        [(string=? "yellow" ls) (...)]
        [(string=? "green" ls) (...)]))
;; Template rules used:
;;  - one of: 3 cases
;;  - atomic distinct: "red"
;;  - atomic distinct: "yellow"
;;  - atomic distinct: "green"

 

 

 

 

| Itemization

An itemization describes data comprised of 2 or more subclasses, at least one of which is not a distinct item. (C.f. enumerations, where the subclasses are all distinct items.) 

 

Functions operating on itemizations should have at least as many tests as there are cases in the itemizations. If there are intervals in the itemization, then there should be tests at all points of variance in the interval. In the case of adjoining intervals it is critical to test the boundaries. (모든 경계에 있는 데이터를 다 테스트해봐야 한다)

서로 다른 타입의 데이터를 산출하는 경우

 

 

 

 

; Problem
; 
; ;Design a data definition to represent the cureent state of the countdown,
; ;which falls into one of three categories
; 
; ;-not yet started
; ;-from 10 to 1 seconds before midnight
; ;-complete (Happy New Year!)



;; (type comment) CountDown is one of:
;; - false (distinct, countdown not started)
;; - Natural[1, 10]
;; - "complete"
;; interp.
;;   false means countdown has not yet started
;;   Natural[1, 10] means countdown is running and how many seconds left
;;   "complete" means countdown is over

;;(examples)
(define CD1 false)
(define CD2 10) ;just started running
(define CD3 1) ; almost over
(define CD4 "complete")

#;
;;(template)
(define (fn-for-countdown c)
  (cond [(false? c) (...)]
        [(and (number? c) (<= 1 c) (<= c 10)) (... c)] ;no need to bother bounding the numbers
        [else (...)]))

;;Template rules used:
;; - one of : 3 cases
;; - atomic distinct : false
;; - atomic non-distinct : Natural[1, 10]
;; - atomic distinct : "complete"

 

 

 

굳이 하나의 데이터 타입만 남았을 때, 타입을 확인안해도 되는 식으로 효율화 가능

 

; Problem
; Design a data definition for a traffic light that can either be disabled,
; or be one of red, yellow or green.


;;Data definition;

;;TLight is one of:
;;- false
;;- "red"
;;- "yellow"
;;- "green"
;; interp. false means the light is disabled, otherwise the color of the light
(define TL1 false)
(define TL2 "red")

#;
(define (fn-for-tlight t1)
  (cond [(false? t1) (...)]
        [and (string? t1) (string=? t1 "red") (...)]
        [and (string? t1) (string=? t1 "yellow") (...)]
        [and (string? t1) (string=? t1 "green") (...)]))

; However, after false, there's only string type left, so no need to ask
(define (fn-for-tlight t1)
  (cond [(false? t1) (...)]
        [(string=? t1 "red") (...)]
        [(string=? t1 "yellow") (...)]
        [(string=? t1 "green") (...)]))



;;Template rules used:
;;- one of: 4 cases
;;- atomic distinct: false
;;- atomic distinct: "red"
;;- atomic distinct: "yellow"
;;- atomic distinct: "green"

 

 


 

 

;  Problem
; use LetterGrade data definition below, design a function that consumes a letter grade
; and produces the next highest letter grade.


;; Data definitions:
;; LetterGrade is one of:
;;- "A"
;;- "B"
;;- "C"
;;interp. the letter grade in a course
;; <examples are redundant for enumerations>

(define (fn-for-letter-grade lg)
  (cond [(string=? lg "A") (...)]
        [(string=? lg "B") (...)]
        [(string=? lg "C") (...)]))

;;Template rules used:
;;one-of: 3 cases
;; atomic distinct: "A"
;; atomic distinct: "B"
;; atomic distinct: "C"


;;Functions
;;LetterGrade --> LetterGrade
;;produce next higest lettre grade (no change for A)

(check-expect (bumpup "A") "A")
(check-expect (bumpup "B") "A")
(check-expect (bumpup "C") "B")

;(define (bumpup lg) "A");stub
;<use template from LetterGrade>
(define (bumpup lg)
  (cond [(string=? lg "A") "A"]
        [(string=? lg "B") "A"]
        [(string=? lg "C") "B"]))

 

 

Data Definition에 따라 function을 더 잘 짤 수 있다 

 

 

 

 

내가 이 코딩이 재밌는 이유 중 하나는, 실수가 당연하다는 거

아무리 숙달된 사람들도 언제나 버그에 마주치고, 항상 실수하고 에러가 난다는 거

그래도 우리가 할 일은, '나는 바보 멍충이 해삼 말미잘이야'하면서 이불 속으로 웅크리는 게 아니라

'당연한거야 괜찮아, 다시 돌려보면 돼' 하고 다시 코드를 고쳐보는거다.