Lisp 주석 규칙
여러 종류의 주석에 사용할 세미콜론 수에 대한 Lisp 규칙은 무엇입니까 (다양한 세미콜론 수에 대한 들여 쓰기 수준)?
또한 세미콜론 주석을 사용할 때와 사용할 때 #|multiline comments|#
(여러 구현에 존재하고 존재한다고 가정) 에 대한 규칙이 있습니까?
Common Lisp에서 :
;;;; At the top of source files
;;; Comments at the beginning of the line
(defun test (a &optional b)
;; Commends indented along with code
(do-something a) ; Comments indented at column 40, or the last
(do-something-else b)) ; column + 1 space if line exceeds 38 columns
참고 : Emacs는 글꼴 모양이 좋지 #| |#
않지만 Rainer가 주석에서 제안한대로 #|| ||#
대신 사용해보십시오 .
나는 이것을 사용하는 규칙이 없다고 말하고 싶지만 엄청난 양의 코드에 주석을 달거나 거대한 BNF 목록 등과 같이 세미콜론이 편집을 방해하는 긴 설명을 삽입하는 것이 더 빠르다고 생각합니다.
표현식에 #+(or)
다음 과 같은 접두사를 붙이는 코드를 비활성화하는 깔끔한 트릭이 있습니다 .
(defun test (a &optional b)
#+(or)
(do-something a)
(do-something-else b))
참고 : 또는 기능 #+nil
이없는 경우 일반적으로 작동 합니다. 의 장점은 주석을 달거나로 변경하여 쉽게 편집 할 수 있으며 실제로 해당 표현식을 읽을 수있는 기능 세트를 실제로 포함 할 수 있다는 것입니다.nil
:nil
#+(or)
#+(and)
SLIME은 (do-something a)
Lisp를 실행할 때 양식 을 주석 으로 글꼴 화하여 여기에서 도움이됩니다 .
Common Lisp의 특정 주석 구문 및 트릭 (예 : #| |#
and #+(or)
또는 더 일반적으로 볼 수 있음 #+nil
) 외에도 세미콜론 규칙이 다른 lisps에서도 널리 채택되고 있다고 생각합니다.
다음 은 사양 에서 발췌 한 것입니다 . 단일 세미콜론과 관련하여 현재 관행이 어떻게 달라 졌는지 확인하세요.
2.4.4.2 세미콜론 스타일에 대한 참고 사항
일부 텍스트 편집기는 주석을 시작하는 세미콜론 수를 기반으로 원하는 들여 쓰기에 대해 가정합니다. 다음 스타일 규칙은 보편적이지 않지만 일반적입니다.
2.4.4.2.1 단일 세미콜론 사용
단일 세미콜론으로 시작하는 주석은 모두 오른쪽의 동일한 열 ( "주석 열"이라고도 함)에 정렬됩니다. 이러한 주석의 텍스트는 일반적으로 해당 주석이 나타나는 줄에만 적용됩니다. 때때로 두세 개가 한 문장을 함께 포함합니다. 이것은 때때로 추가 공백 (세미콜론 뒤)으로 첫 번째를 제외하고 모두 들여 쓰기로 표시됩니다.
2.4.4.2.2 이중 세미콜론 사용
이중 세미콜론으로 시작하는 주석은 모두 양식이 코드에서 동일한 위치에있는 것과 동일한 수준의 들여 쓰기로 정렬됩니다. 이러한 주석의 텍스트는 일반적으로 주석이 발생한 시점의 프로그램 상태, 주석 다음에 오는 코드 또는 둘 다를 설명합니다.
2.4.4.2.3 트리플 세미콜론 사용
트리플 세미콜론으로 시작하는 주석은 모두 왼쪽 여백에 정렬됩니다. 일반적으로 정의 내에서가 아니라 정의 또는 정의 세트 이전에 사용됩니다.
2.4.4.2.4 4 중 세미콜론 사용
4 중 세미콜론으로 시작하는 주석은 모두 왼쪽 여백에 정렬되며 일반적으로 다음 코드의 제목으로 사용되는 짧은 텍스트 만 포함되며 코드를 준비하는 프로그램의 머리글 또는 바닥 글에 사용될 수 있습니다. 하드 카피 문서로 표시하기 위해.
2.4.4.2.5 세미콜론 스타일의 예
;;;; Math Utilities ;;; FIB computes the the Fibonacci function in the traditional ;;; recursive way. (defun fib (n) (check-type n integer) ;; At this point we're sure we have an integer argument. ;; Now we can get down to some serious computation. (cond ((< n 0) ;; Hey, this is just supposed to be a simple example. ;; Did you really expect me to handle the general case? (error "FIB got ~D as an argument." n)) ((< n 2) n) ;fib[0]=0 and fib[1]=1 ;; The cheap cases didn't work. ;; Nothing more to do but recurse. (t (+ (fib (- n 1)) ;The traditional formula (fib (- n 2)))))) ; is fib[n-1]+fib[n-2].
여러 줄 주석 # | | #은 더 많은 양의 Lisp 코드 또는 예제 코드를 주석 처리하는 데 자주 사용됩니다. 일부 Emacs 구현은 구문 분석에 문제가있는 것 같으므로 일부는 # || 대신 || #.
세미콜론의 사용에 대해서는 Guy L. Steele Jr. 의 책 Common Lisp the Language (348 페이지), 1984, Digital Press 의 주석 예제를 참조하십시오 .
;;;; COMMENT-EXAMPLE function.
;;; This function is useless except to demonstrate comments.
;;; (Actually, this example is much too cluttered with them.)
(defun comment-example (x y) ;X is anything; Y is an a-list.
(cond ((listp x) x) ;If X is a list, use that.
;; X is now not a list. There are two other cases.
((symbolp x)
;; Look up a symbol in the a-list.
(cdr (assoc x y))) ;Remember, (cdr nil) is nil.
;; Do this when all else fails:
(t (cons x ;Add x to a default list.
'((lisp t) ;LISP is okay.
(fortran nil) ;FORTRAN is not.
(pl/i -500) ;Note that you can put comments in
(ada .001) ; "data" as well as in "programs".
;; COBOL??
(teco -1.0e9))))))
이 예에서 주석은 1-4 개의 세미콜론으로 시작할 수 있습니다.
단일 세미콜론 주석은 모두 오른쪽의 동일한 열에 정렬됩니다. 일반적으로 각 주석은 옆에있는 코드에만 관련됩니다. 때로는 주석이 2 ~ 3 줄을 차지할만큼 길기도합니다. 이 경우 주석의 연속 된 줄을 세미콜론 뒤에 한 칸 들여 쓰는 것이 일반적입니다.
Double-semicolon comments are aligned to the level of indentation of the code. A space conventionally follows the two semicolons. Such comments usually describe the state of the program at that point or the code section that follows the comment.
Triple-semicolon comments are aligned to the left margin. They usually document whole programs or large code blocks.
Quadruple-semicolon comments usually indicate titles of whole programs or large code blocks.
The standard reference for Common Lisp style, including commenting conventions, is Peter Norvig and Kent Pitman's Tutorial on Good Lisp Programming Style.
Instead of describing it here, have a look at this page. It's talking about Emacs Lisp, but the convention are the same across all lisps (and schemes).
It's annoying that people refer to conventions without explaining what's wrong with using double semicolons with end-of-line-comments.
There's nothing wrong per-se with using double semicolons with so called "margin" (end-of-line) comments. It may become a problem though if you want to have margin comments and regular comments in the same block, e.g.:
(defn foo []
(bar) ;; yup, bar
;; let's now do a zap
(zap))
So, if you use fill-paragraph
feature of Emacs - it will automatically align both those comments as if they were a single statement.
(defn foo []
(bar) ;; yup, bar
;; let's now do a zap
(zap))
And that's not what you probably want. So if use a single semicolon instead:
(defn foo []
(bar) ; yup, bar
;; let's now do a zap
(zap))
It will keep it as intended. So instead explaining this over and over again, I guess people just made a rule - use single semicolon for margin comments
참고URL : https://stackoverflow.com/questions/6365334/lisp-commenting-convention
'Nice programing' 카테고리의 다른 글
IntelliJ IDEA-캐럿 동작 (0) | 2020.12.04 |
---|---|
배열 배열을 평면화하는 방법-끝까지 내려 가지는 않음 (0) | 2020.12.04 |
SQL Server의 모든 현재 임시 테이블 목록을 가져 오는 방법이 있습니까? (0) | 2020.12.04 |
GCC가 바쁜 대기 루프를 최적화하지 못하게하는 방법은 무엇입니까? (0) | 2020.12.04 |
Python simpleHTTPserver를 어떻게 종료합니까? (0) | 2020.12.04 |