19. 查询序列中最后一个元素
Description:Write a function which returns the last element in a sequence.
(= (__ (list 1 2 3 4 5)) 4)
(= (__ ["a" "b" "c"]) "b")
(= (__ [[1 2] [3 4]]) [1 2])
第一种:使用功能nth() dec() count()
三个函数联合使用,
//1.先算出序列中的总长度(count %)=>5
//2.倒数第二个数,则用总长度减1 (dec (count %))=>4
//3.指定序列中第n个数找出需要的数字 (nth 4)=>4
#(nth % (dec (count %)))
第二种:使用last()函数
(last)
20. 查询序列中倒数第二个元素
Description:Write a function which returns the second to last element from a sequence.
使用: reverse(),second(),comp()
三个函数联合使用
(= (__ (list 1 2 3 4 5)) 4)
(= (__ ["a" "b" "c"]) "b")
//1.把序列的顺序反转过来 (reverse)=> 5 4 3 2 1
//2