Programming 515

(Julia) 매크로

Julia에서는 Lisp의 매크로와 비슷하게 매크로를 사용할 수 있다. Metaprogramming · The Julia Language :()로 quote하고, $로 unquote한다. 예제: julia> :($1+1) :(1 + 1) julia> :($(1+1)) 2 julia> macro aif(a,b,c) :(if $a; $b else $c end) end @aif (macro with 1 method) julia> @aif 1 2 3 ERROR: TypeError: non-boolean (Int64) used in boolean context Stacktrace: [1] top-level scope @ REPL[6]:1 julia> @macroexpand @aif 1 2 3 :(if 1 #= R..

Programming 2024.01.26

백만 이하로 시작하는 우박수 중 가장 긴 과정을 거치는 것은?

백만 이하로 시작하는 우박수 중 가장 긴 과정을 거치는 것은? (tistory.com) 백만 이하로 시작하는 우박수 중 가장 긴 과정을 거치는 것은? 사이트 이름 - 문제 14번 백만 이하로 시작하는 우박수 중 가장 긴 과정을 거치는 것은? 양의 정수 n에 대하여, 다음과 같은 계산 과정을 반복하기로 합니다. n → n / 2 (n이 짝수일 때) n → 3 * n + 1 ( thisblogbusy.tistory.com Common Lisp: (let ((hh (make-hash-table))) (defun hailstone (n) (if (= n 1) (return-from hailstone 1)) (let ((h (gethash n hh))) (if h h (setf (gethash n hh) (1+ ..

Programming 2024.01.09

(Racket) 매크로 정복하기

Racket 매크로는 hygienic macro인데, 쓰기 더 어려운 면이 있다. s-expression을 데이터로 바로 조작하기가 어렵다. syntax->datum과 datum->syntax를 하여 번거롭게 해야 한다. (` 대신 #`를 사용하면 datum->syntax를 안 해도 되기는 하다.) 그러던 와중, Lisp: Common Lisp, Racket, Clojure, Emacs Lisp - Hyperpolyglot 에서 Racket의 define-syntax-rule 예제를 보게 되었다. Lisp의 defmacro와 비슷하게 구현하였는데, 다음과 같다: (define-syntax-rule (rpn3 arg1 arg2 op) (eval ‘(,op ,arg1 ,arg2))) 위 코드는 REPL에서는..

Programming 2023.10.30