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

            久久精品人人做人人爽电影蜜月| 人妻精品久久久久中文字幕| 无码精品久久久久久人妻中字| 久久久久久九九99精品| 久久精品国产亚洲av麻豆色欲 | 国产人久久人人人人爽| 99麻豆久久久国产精品免费| 久久黄色视频| 久久人人添人人爽添人人片牛牛| 久久久国产99久久国产一| 久久久久高潮毛片免费全部播放 | 9久久9久久精品| 久久久精品久久久久久| 国产69精品久久久久久人妻精品| 久久久久免费精品国产| 久久国产亚洲精品| 久久精品国产黑森林| 国产情侣久久久久aⅴ免费| 色综合合久久天天给综看| 色8久久人人97超碰香蕉987| 久久久久久久亚洲精品| 精品综合久久久久久97超人| 久久精品国产99久久久古代 | 久久乐国产综合亚洲精品| 欧美亚洲国产精品久久蜜芽| 亚洲av日韩精品久久久久久a| 久久婷婷五月综合97色直播 | 人妻无码精品久久亚瑟影视| 国产—久久香蕉国产线看观看| 久久久久久久尹人综合网亚洲| 久久亚洲欧洲国产综合| 久久久久亚洲?V成人无码| 大蕉久久伊人中文字幕| 国内精品久久久久影院日本| 久久精品国产亚洲av水果派| 国内精品久久久久影院日本 | 久久综合久久综合亚洲| 色综合久久中文字幕综合网| 久久国产精品免费一区二区三区| 亚洲国产成人久久综合碰碰动漫3d| 国内精品久久久人妻中文字幕|