Why does array.slice behave differently for (length, n)

前端 未结 3 1672
旧时难觅i
旧时难觅i 2020-12-05 14:01

If I have an array a:

  1. a[a.length] returns nil. Good.
  2. a[a.length, x] returns []. Good.
3条回答
  •  旧时难觅i
    2020-12-05 14:48

    Look to your friendly Lispy languages for the answer. The philosophy you're looking for began with languages whose specialty was LISt Processing. For instance, here's one way of creating lists in Haskell:

    1:[] => [1] 
    1:2:3:[] => [1,2,3] 
    

    This is called cons-ing, for 'constructing' a list. If the idea hasn't clicked yet, consider this: an array is created by adding elements to an empty list, not to 'nil'.

提交回复
热议问题