νμ€ν μΉπ κ°λ°μ μ§λ§μ π§π½βπ»
β μΈκ³΅μ§λ₯ κ΄μ¬ π€
Categories
-
β£
βΆ COMPUTER_SCIENCE
π: 7 -
β£
βΆ WEB
π: 3 -
β£
βΆ ETC
π: 3-
β
β£
ETCS
π: 10 -
β
β£
SUBBRAIN κ°λ°κΈ°
π: 5 -
β
β
YOS κ°λ°κΈ°
π: 1
-
β
β£
-
β
βΆ AI
π: 9-
β£
AITOOLS
π: 3 -
β£
CV
π: 2 -
β£
DEEP_LEARNING
π: 1 -
β£
DATA_VIS
π: 2 -
β£
GRAPH
π: 1 -
β£
LIGHTWEIGHT
π: 1 -
β£
MATH
π: 1 -
β£
NLP
π: 3 -
β
STRUCTURED_DATA
π: 2
-
β£
OSSU PL-B Section 1
μΆμ² κ°μ Programming Languages, Part A, μμ±ν΄ λνκ΅
OSSU PL-B Section 1
κ°λ¨ν Racket μ€λͺ
μ΄λ―Έ μμ Racektμ μ΄μ©ν΄λ΄€κΈ° λλ¬Έμ κ°λ¨ν μ€λͺ νλ€.
- λμ νμ΄νμ μ§μ
- λ°λΌμ
cond
μnumber?
λ±μ μ΄μ©ν΄ λΆκΈ° μ²λ¦¬ κ°λ₯
- λ°λΌμ
- κ³Όκ±° μ΄λ¦μ
Scheme
μ΄μμΌλ©°, λΉ λ¦¬μ€νΈ,cons
, mutable λ±μ λ³νκ° μ΄λ€μ§ λ€ RacketμΌλ‘ λ°λμλ€. #lang racket
μΌλ‘ μμν κ²
|Primitive|Description|Example|
|β|β|β|
|null
|λΉ λ¦¬μ€νΈ|null
|
|car
|리μ€νΈ 첫λ²μ§Έ μμ|(car some-list)
|
|cdr
|리μ€νΈ λλ¨Έμ§ μμλ€|(cdr some-list)
|
|null?
|#t
=λΉ λ¦¬μ€νΈ,#f
=λλ¨Έμ§|(null? some-value)
|
let
, let*
, letrec
etc.
(let ([local-var local-val] ..) e)
: μ§μ μ€μ½ν μ€μ , μ΄μ λ°μΈλ© μ¬μ© λΆκ°λ₯
(let* ([local-var local-val] ..) e)
: μ΄μ μ§μ μ€μ½ν λ°μΈλ© μ¬μ© κ°λ₯
(letrec ([local-var local-val] ..) e)
: μμμ μκ΄ μλ μ§μ μ€μ½ν(μνΈ μ¬κ· κ°λ₯)
set!
: μ΄λ―Έ λ°μΈλ©λ λ³μλ₯Ό λ³κ²½ν¨
mcons
: λ³κ²½ κ°λ₯ν pair μμ±
mcar
,mcdr
,mpair?
λ±μ μ΄μ© ν΄μΌν¨set-mcdr!
,set-mcar!
λ±μ μ΄μ©ν΄ λ³κ²½νν μ§μ°κ³Ό Thunk
λͺ¨λ νν
e
λ 곧λ°λ‘ ννλ νμκ° μλ€. μλ₯Ό λ€μ΄(if e1 e2 e3)
μμe1
μ΄#t
μ΄λ©΄e3
κ°μ μ¬μ©λμ§ μμΌλ―λ‘ ννλμ§ μλλ€.- λ¨μν μ±λ₯ λΏλ§ μλλΌ, 무ν 루ν λ±μ λ§κΈ° μν΄ νμνλ€.
νμ§λ§ μ΄λ ν¨μμ μΈμ μ λ¬ λ±μλ μ μ©λμ§ μλλ€. λ°λΌμ μλ―Έμλ ννμ λ§κΈ° μν΄ ν¨μμ λ°λλ ν¨μκ° μ¬μ©λκΈ° μ κΉμ§ ννλμ§ μλ νΉμ±μ μ΄μ©νλ κ²μ ThunkλΌκ³ νλ€.
e
λ₯Ό κ·Έλλ‘ μ λ¬νμ§ μκ³ , (define a (lambda () e))
νμμΌλ‘ μ λ¬ν λ€, μ¬μ©ν λ (a)
λ₯Ό μ΄μ©ν΄ e
λ₯Ό νννκ² νλ€.
μ΄λ κ² ννλ e
λ μΈμ λ μ¦μ ννλ기보λ€λ, ννλ ν, κ²°κ³Όλ₯Ό μ μ₯νμ¬ μ¬μ©νλ νΈμ΄ ν¨μ¨μ μ΄λ©°, μ΄λ₯Ό Delayμ ForceλΌκ³ νλ©°, λ€μκ³Ό κ°μ ν¨μλ‘ κ΅¬ν κ°λ₯νλ€.
; fλ (lambda () e) νμμΌλ‘ μ ν΄μ£Όλ©΄ λ¨
(define (my-delay f) (mcons #f f))
(define (my-force th)
(if (mcar th)
(mcdr th)
(begin (set-mcar! th #t) (set-mcdr! th ((mcdr th)))
(mcdr th))))
Stream
무νν μ ν΄μ§λ λ°μ΄ν°λ₯Ό νν(ex) Unix pipe)ν λ μ¬μ©νλ©°, μ΄λ₯Ό μμ λ°°μ΄ Delayμ Force, Thunkλ₯Ό μ΄μ©ν΄ μ½κ² ννν μ μλ€.
; (cons cur-val next-val-thunk) νμμΌλ‘ μμ±
(define (stream-maker fn arg)
(letrec
([f (lambda (x)
(cons x (lambda () (f (fn x arg)))))])
(lambda () (f arg))))
(define ones
(stream-maker (lambda (x y) 1) 1))
(define nats (stream-maker + 1))
(define powers-of-two (stream-maker * 2))
Memoization
κ°μ μ μ₯μ ν΅ν΄ ν¨μ¨μ μΈ μ°μ°μ μννλ λ²
(define fibonacci
(letrec([memo null]
[f (lambda (x)
(let ([ans (assoc x memo)]) # assoc : κ° xλ₯Ό 리μ€νΈ memoμμ μ°Ύμλ
(if ans
(cdr ans)
(let ([new-ans (if (or (= x 1) (= x 2))
1
(+ (f (- x 1)) (f (- x 2))))])
(begin (set! memo (cons (cons x new-ans) memo)) new-ans)))))])
f))
Macros
Racketμ Macroλ C/C++
μ λΉν΄ μ¬μ©νκΈ° μ½λ€.
- lexical scopeμ μλ μ μΌ μ§μ λ³μ μ μ©
- μ¬κ· Macro μ¬μ© κ°λ₯
- ν¨μ λ΄ λ‘컬 μ€μ½ν λ±μΌλ‘ Macroλ₯Ό shadowing κ°λ₯
(define-syntax
my-if (syntax-rules (then else)
[(my-if e1 then e2 else e3) (if e1 e2 e3)]))
맀ν¬λ‘λ₯Ό μ΄μ©ν΄ for-loop λ±μ μμ±ν μ μλ€.(whileμ λΆκ°λ₯νλ€κ³ ν¨?)
νμ§λ§ λ€μκ³Ό κ°μ μ΄μ λ‘ μ¬μ©μ μ£Όμν΄μΌ νλ€.
- 맀ν¬λ‘ λ΄ νν
e
μ νν μμ () - ννμ μλ―Έμλ λ°λ³΅ μ°μ°
- Tokenization : μ νν 곡백μΌλ‘ λλμ΄μ§ λ¨μ΄λ₯Ό μ€μ¬μΌλ‘ λ³κ²½ μν΄, ex) (token 1) 맀ν¬λ‘λ tokenizationμ 1izationμΌλ‘ λ°κΎΈμ§ μλλ€. λ€λ§ this is tokenμ this is 1 μΌλ‘ λ°κΎΌλ€.
_articles/computer_science/OSSU/PL/PLs/OSSU PL-B/OSSU PL-B Section 1.md