• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Life is Good.

            Enhance Tech and English
            隨筆 - 65, 文章 - 20, 評(píng)論 - 21, 引用 - 0
            數(shù)據(jù)加載中……

            How to use (nth …) Safely

            Danger of the (nth …) Function

             

            If not used carefully, this function can easily generate “bad argument” errors in our product. The purpose of this function is to return the “nth” index entry of a list. The syntax is (nth ix lst) where “ix” the index (zero-based) and “lst” is a non-nil list.

             

            So, if the “lst” is a four-element list such as (“apple” 3.14 (“banana” “grape”) “CR101”) then the nth function will return the following:

             

            (nth 0 lst) returns “apple”

            (nth 1 lst) returns 3.14

            (nth 2 lst) returns (“banana” “grape”)  - i.e. a sublist in the list

            (nth 3 lst) returns “CR101”

            (nth 4 lst) returns nil

            (nth 5 lst) returns nil

             

            BUT, if “lst” does not exist (i.e. “lst” is nil), then any of the above calls will trigger our product to fail with a “bad argument” message in the command window. You can simulate this by trying it at your command line. Type this (setq xx nil) [Enter] and then this (nth 0 xx) [Enter].

             Under some conditions, the “xx” list above comes through as undefined or nil. So, when the (nth …) function tries to return the 2nd element (index = 1) of this non-existent list, our product fails with a “bad argument” message.

             

            How to use (nth …) Safely

             

            Solution #1 – use (car lst), (cadr lst), (caddr lst) instead of (nth 0 lst), (nth 1 lst), (nth 2 lst) – these “ca*” functions extract the same information from the list but do not fail with a “bad argument” message if the lst does not exist. So, in the above example, (setq typeflag (cadr xx)) would return the same information as (setq typeflag (nth 1 xx)) BUT without the chance of failing if the list “xx” did not exist. So, (nth 1 xx) fails with “bad argument” but (cadr xx) returns safely with a returned value of nil.

             

            Solution #2 – add an error check prior to calling the (nth…) function. In the above example, do something like this:  (if xx (setq typeflag (nth 1 xx))). This expression will only execute the (nth…) function if “xx” is non-nil. If you really want to make sure that all is well, that xx is non-nil AND it is a “List”, you could do this:  (if (= (type xx) ‘LIST)(setq typeflag (nth 1 xx))). But, in general, the first example is probably sufficient.

            posted on 2008-07-31 14:57 Mike Song 閱讀(102) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            亚洲AV无码久久精品蜜桃| 国产精品9999久久久久| 欧美日韩成人精品久久久免费看| 久久精品无码一区二区三区免费| 久久久久免费视频| 国产精品久久新婚兰兰| AV无码久久久久不卡蜜桃| 亚洲综合久久综合激情久久| 久久伊人色| 久久九九青青国产精品| 无码八A片人妻少妇久久| 久久久久久综合一区中文字幕| 武侠古典久久婷婷狼人伊人| av无码久久久久不卡免费网站| 青青青青久久精品国产h久久精品五福影院1421| 亚洲精品视频久久久| 91久久国产视频| 久久精品a亚洲国产v高清不卡| 久久人人爽人人爽AV片| 99久久免费国产精品热| 狠狠综合久久综合88亚洲| 久久久久九国产精品| 99久久综合狠狠综合久久| 国内精品久久久久久99蜜桃| 国内精品综合久久久40p| 亚洲国产高清精品线久久| 精品久久久久久国产牛牛app| 国内精品久久久久影院优| 国产A级毛片久久久精品毛片| 久久久网中文字幕| 国产免费久久精品99久久| 91精品日韩人妻无码久久不卡| 国产精品久久久久久吹潮| 久久久一本精品99久久精品88 | 国产精品久久久亚洲| 一本色道久久88精品综合| 国产成人无码精品久久久性色| 久久这里有精品| 日韩人妻无码精品久久久不卡| 亚洲人成精品久久久久| 久久免费的精品国产V∧|