• <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无码成人网站久久精品大| 久久久久亚洲av成人网人人软件 | 国产高清国内精品福利99久久| 久久精品中文字幕有码| 久久亚洲精品国产亚洲老地址| 五月丁香综合激情六月久久| 精品999久久久久久中文字幕| 青青草国产97免久久费观看| 久久精品国产第一区二区三区| 99久久婷婷国产一区二区| 偷窥少妇久久久久久久久| 久久青草国产精品一区| 久久99久久99精品免视看动漫| 91久久精品视频| 亚洲午夜久久久影院| 久久精品?ⅴ无码中文字幕| 一本久久知道综合久久| 国产精品无码久久综合网| 久久综合香蕉国产蜜臀AV| 久久久无码精品亚洲日韩软件| 久久精品国产半推半就| 中文字幕久久精品无码| 亚洲国产精品嫩草影院久久| 国产精品激情综合久久| 久久人人爽人人爽人人片av高请| 91精品国产91久久久久久青草| 欧美精品九九99久久在观看| 久久九色综合九色99伊人| 狠狠色综合网站久久久久久久高清 | 亚洲性久久久影院| 国产精品福利一区二区久久| 精品欧美一区二区三区久久久| 一本色综合网久久| 日韩十八禁一区二区久久 | 九九久久99综合一区二区| 国产精品久久新婚兰兰| 日本久久久精品中文字幕| 久久热这里只有精品在线观看| 大香网伊人久久综合网2020|