Localising Variables 局部變數

來源:互聯網
上載者:User

標籤:style   blog   http   color   strong   io   for   cti   

 

This tutorial will give a brief overview of the defun function and moreover explain why localising variables is a good habit to get into.

By http://lee-mac.com/localising.html

(defun <symbol> ([arguments] [/ variables]) <expressions>)

 

Here:

  • symbol is the name of the function (if prefixed with ‘c:‘ the function may be called from the command line).
  • arguments are symbols representing data that the function requires when called (more about these some other time).
  • variables are the symbols used to hold data within the function.
  • expressions are all the statements that make up the function itself.
Examples:
(defun test ( arg1 arg2 / var1 var2 ) ;; Function taking two arguments, with two local variables  ;; Expressions  (setq var1 (* arg1 2.0) var2 (+ arg2 3))  (print (- var2 var1))  (princ)  ) ;; End of defun

 

(defun c:MyCommand ( / a b c ) ;; Function with three local variables  (setq a 1.0 b 2 c 3)  (print (- b (/ a c)))  (princ))

 

(defun myfunc ( x y ) ... )         ;; Function takes two arguments(defun myfunc ( / a b ) ... )       ;; Function has two local variables(defun myfunc ( x / temp ) ... )    ;; One argument, one local variable(defun myfunc ( ) ... )             ;; No arguments or local variables

 

 

What are the Consequences?

Take this example (Notice no variables are localised):

 

(defun c:test ( )  (foreach x ‘(1 2 3 4 5)    (setq lst (cons x lst))  )  (print lst)  (princ))

Return:

(5 4 3 2 1)

if we run the code again, we receive:

(5 4 3 2 1 5 4 3 2 1)

 

The list has doubled! This is because the variable lst still holds the list data even after the function has completed.

Hence, when evaluated for the second time, new values are added the existing list.

;;x可認為是foreach的形參,調用的時候IDE自動聲明為局部變數了。

 

This certainly highlights one major issue, but what if we had another function, also without localised variables, using the same symbol for a variable name?

(defun c:test1 ( )  (foreach x ‘("1" "2" "3" "4" "5")    (setq lst (cons x lst))  )  (print lst)  (princ))

Upon running the above code, we now have a list containing a mixture of data types:

("5" "4" "3" "2" "1" 5 4 3 2 1 5 4 3 2 1)
To avoid these issues, we localise the variables:
(defun c:test ( / lst )  (foreach x ‘(1 2 3 4 5)    (setq lst (cons x lst))  )  (print lst)  (princ))

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.