Lisp: S-expressions & Primitive Functions

Functional Programming Language.

Everything in "s-expressions" -- symbolic expressions

All uppercase letters

S-Expressions

Atoms

Integer or symbolic identifier

e.g. 42 XYZ T NIL

T -- true

NIL -- false

Non-atomic

i.e. Binary trees

  • Children can be atom or non-atom
  • Left-half: car
  • Right-half: cdr

e.g. (42.NIL) (NIL.42) (42.(NIL.42)) ((NIL.42).42)

How are s-expressions stored and used?

  • Atoms: one word of memory storing the int value or name of the atom; accessed by a pointer to that memory loaction
  • Non-atoms: one block of two words of memory storing the car & cdr of the non-atom. Accessed by a pointer to the block of two words.
    • car: content of the address register -- the left child
    • cdr: content of the displacement register -- the right child

Primary Functions

  • CAR -- return car of a non-atom, error if param is an atom
  • CDR -- return cdr of a non-atom, error if param is an atom
  • CONS -- combine two atom/non-atom into one non-atom, e.g. CONS[se1, se2] = (se1 . se2)
  1. Grab a two-word block of memory from the heap

  2. Copy se1 into the first word

  3. Copy se2 into the second word

  4. Return a pointer to the two-word block of memory

  • EQ -- compare two atoms, return T if the same, NIL otherwise
    1. EQ [ ae1, ae2 ] -- ae1, ae2 must both be atomic, otherwise error

    2. Compare pointers but not values

  • ATOM [se] -- T if se is atomic, NIL otherwise

  • NULL [se] -- T if se is NIL, NIL otherwise.

Customized Functions

  •  cadaar[se] = car[cdr[car[car[se]]]] -- [se] is the param decl instead of an actual variable
    

** Note, everything in lisp should be s-expressions, but the above is not written in that format. Therefore, the above is actually a pseudo code, and it has to be converted into formal lisp.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值