Lisp: list notation

  • Rationale: too many parentheses in s-exp, list notation simplify the format 
  • Definition: A list is a sequence of either atoms or other lists separated by blanks and enclosed in parentheses.
  • Can be used to create expressions and data structures.

e.g. Representing 40, 10, 50, 20, 30.

  • s-exp:
  •  
  • list-notation

        (40 10 50 20 30) = (40 . (10 . (50 . (20 . (30 . NIL)))))

        ** list notation is essentially a linked list

Format

  1. Can have 0 or more element, i.e. empty list; NIL = () -- note that an empty list is both an atom and a list.  
  2. List items can be atoms, non-atoms, or even lists,

        e.g. ((40 . 10) (50 . 20) 30) or ((40 10) (50 . 20) (70 60) 30) are both legal

Functions

  • Written in a list notation
  • First item of the list is the function name
  • Remaining items of the list are function params

e.g. (f x y) in lisp is equivalent to f(x,y) in other languages

read-eval-print

  1. Lisp read list from user input
  2. Evaluate the function with the arguments
    1. Evaluate arguments
    2. Apply result to function
    3. Do it recursively until all functions evaluated
  3. Print the result to the interpreter console
  4. Loop back to step 1

Math expressions are also examples of functions: 

Math Expressions

(* 7 9) = 7 x 9 = 63

(– (+ 3 4) 7) = 3+4-7 = 0

read-eval-print

  1. Lisp read expressions from user input
  2. Evaluate the expression
  3. Print the result to the interpreter console
  4. Loop back to step 1

Lisp evaluates everything

  • An expression -- return expression result
  • Number (e.g. 5) -- return the number, i.e. 5
  • Bounded symbol (x) -- return value bounded to that symbol
  • Unbounded symbol (x) -- return error, because the symbol cannot be evaluated

Native functions

  • list: takes any number of arguments and constructs a list of those elements
  • nth: takes a number and a list as arguments and return the indicated element of the list
> (list 1 2 3 4 5)
(1 2 3 4 5)

> (nth 2 (list 1 2 3 4 5))
3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值