- ;;pick function will return the nth element in the list l.
- (defun pick (n l)
- ( cond
- ( (null l) l)
- ( (eq n 1) (car l))
- ( t ( pick (- n 1) (cdr l)))
- )
- )
- ;pick demo
- (pick 1 '(4 5 6))
- ; 4
- (pick 4 '(4 5 6))
- ;nil
- ;;leftmost function will return the leftmost atom in a given list l.
- (defun leftmost (l)
- ( cond
- ( (null l) l)
- ( (atom (car l)) (car l))
- ( t (leftmost (car l)))
- )
- )
- ;leftmost demo
- (leftmost '(4 5 6))
- ; 4
- (leftmost '( (4 5) 6 (3 4)))
- ; 4
- (leftmost '( ( (2 3) 4) 5 (6 7)))
- ; 2
- (leftmost '( ( (4) 5) 6 (3 4)))
- ; 4
- ;;car cdr car == cadar
- ;;cdr cdr car == cddar
- (cdr (car (cdr '(9 (3 5) (7 5 (3 4))) ) ) )
- ; 5
- (cdadr '(9 (3 5) (7 5 (3 4))))
- ; 5
- (caadr '( (2 3) (1 2) (5 6 7)))
- ; 1
- (car (car (cdr '((2 3) (1 2) (5 6 7)))))
- ; 1
- ;; declare x = 2
- (set 'x 2)
- x
- ; 2
- ;; set + quote so you don't need to put the quote before y
- (setq y 3)
- y
- ; 3
- (+ x y)
- ; 5
- (* x y)
- ; 6
- ;;you can find a car/cdr of a generated list
- (car (list 3 4 'a 'b 6))
- ; 3
- ;;condition with manipulation
- ( cond
- ( (> x 5) (+ x 1))
- ( t (- x 1))
- )
- ; 1
- ;;conditions and or not
- (if (> x 5) (+ x 1) (- x 1))
- ; 1
- (if (not (or (> x 5) (< x 0))) (+ x 1) (- x 1))
- ; 3
- ;same as before with cond
- ;; display maximum item of list
- (max 3 5)
- ; 5
- ;; apply operator
- (apply '+ '(3 4 5))
- ; 12
- ;;you can substitute ' with quote
- (setq op '*)
- ; *
- ;;substitute our variable op for *
- (apply op '(3 4 5))
- ; 60
- ;;same but with max
- (setq op2 'max)
- ; max
- (apply op2 '(3 4 5))
- ; 5
- (apply op2 '(4 7 3))
- ; 7
- ;; returns true if is a number
- (numberp 4)
- ; t
- (numberp 'a)
- (numberp '(3 4))
- ; nil
- (numberp 100)
- ; t
- ;;returns items in list that aren't numbers
- (defun no-num (l)
- ( cond
- ( (null l) l)
- ( (numberp (car l)) (no-num (cdr l)))
- ( t (cons (car l) (no-num (cdr l))))
- )
- )
- (no-num '(1 'a 'b 3 'flower))
- ; ('a 'b 'flower)
- (no-num '(3 5 6))
- ; nil
- x
- ; 2
- y
- ; 3
- (no-num '(a b c x y))
- ; (a b c x y)
- (no-num '(5 'a 'b 'flower 100 15 'car))
- ; ('a 'b 'flower 'car)