• <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, 評論 - 21, 引用 - 0
            數據加載中……

            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 閱讀(101) 評論(0)  編輯 收藏 引用

            精品免费久久久久久久| 久久精品成人| 国产午夜免费高清久久影院| 伊人色综合久久天天人手人婷| 久久精品99久久香蕉国产色戒 | 国产精品一区二区久久精品| 91视频国产91久久久| 国产精品无码久久四虎| 午夜精品久久影院蜜桃| 国内精品久久久久影院一蜜桃| 久久偷看各类wc女厕嘘嘘| …久久精品99久久香蕉国产| 久久精品这里只有精99品| 国产精品久久久久a影院| AV狠狠色丁香婷婷综合久久 | 婷婷综合久久中文字幕蜜桃三电影| 久久夜色精品国产网站| 精品999久久久久久中文字幕| 久久久久久精品久久久久| 久久精品一区二区三区不卡| 亚洲成av人片不卡无码久久| 东京热TOKYO综合久久精品 | 成人妇女免费播放久久久| 欧美激情精品久久久久久| 亚洲色欲久久久综合网| 色青青草原桃花久久综合| 久久综合丁香激情久久| 一本色综合网久久| 久久久久久亚洲精品不卡 | 亚洲人成无码www久久久| 久久精品国产精品青草| 人妻精品久久无码区| 人妻无码精品久久亚瑟影视| 青青热久久国产久精品 | 久久久久人妻精品一区三寸蜜桃| 久久久久成人精品无码中文字幕| 97视频久久久| 亚洲精品国产自在久久| 久久99精品久久久久久不卡| 狠狠精品久久久无码中文字幕 | 欧美亚洲国产精品久久蜜芽|