页面

Makefile里命令解释及转义

新安装的 org-mode 7.5有一个warnings.
故想uninstall之,没想到没有make uninstall.
自己添加了一个.
install规则是:


install: install-lisp

doc: doc/org.html doc/org.pdf doc/orgcard.pdf doc/orgcard_letter.pdf doc/orgguide.pdf doc/orgcard.txt

p:
        ${MAKE} pdf && open doc/org.pdf

g:
        ${MAKE} pdf && open doc/orgguide.pdf

install-lisp: $(LISPFILES) $(ELCFILES)
        if [ ! -d $(lispdir) ]; then $(MKDIR) $(lispdir); else true; fi ;
        $(CP) $(LISPFILES)  $(lispdir)
        $(CP) $(ELCFILES)   $(lispdir)

照样写一个uninstall:


uninstall: uninstall1 uninstall2

uninstall1:
        for f in $(LISPFILES); do \
        if [ -f $(lispdir)/`basename $$f` ]; then rm $(lispdir)/`basename $$f`; fi;\
        done

uninstall2:
        for f in $(ELCFILES); do \
        if [ -f $(lispdir)/`basename $$f` ]; then rm $(lispdir)/`basename $$f`; fi;\
        done
注意看是 $$f, 这是为了转义$符号. 

具体的要翻文档了.


bash命令行的执行过程

~$ name=ngn echo $name
这个不能输出ngn,而只是一个空行.

翻bash的手册找到这么一段:
3.7.1
When a simple command is executed, the shell performs the following expansions, assignments, and redirections, from left to right.
  1. The words that the parser has marked as variable assignments (those preceding the command name) and redirections are saved for later processing.
  2. The words that are not variable assignments or redirections are expanded (see Shell Expansions). If any words remain after expansion, the first word is taken to be the name of the command and the remaining words are the arguments.
  3. Redirections are performed as described above (see Redirections).
  4. The text after the ‘=’ in each variable assignment undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable.
原来echo的参数 $name 在 name=ngn 这个赋值之前就已经被扩展了.这时自然是空了.所以不会输出内容.

验证:
~$ name=ngn perl -e 'print $ENV{name}'
ngn
~$

sicp 1.16的答案

看了sipc前面一两节, 主要就明白了迭代和循环的不同.

习题1.16
 Design a procedure that evolves an iterative exponentiation process that uses successive squaring and uses a logarithmic number of steps, as does fast-expt. (Hint: Using the observation that (bn/2)2 = (b2)n/2, keep, along with the exponent n and the base b, an additional state variable a, and define the state transformation in such a way that the product a bn is unchanged from state to state. At the beginning of the process a is taken to be 1, and the answer is given by the value of a at the end of the process. In general, the technique of defining an invariant quantity that remains unchanged from state to state is a powerful way to think about the design of iterative algorithms.)

用迭代实现的一个exp

(define square
  (lambda (x)
    (* x x)))

(define exp-iter
  (lambda (base n product)
    (cond ((= n 0) product)
          ((even? n) (exp-iter (square base) (/ n 2) product))
          (else (* base (exp-iter base (- n 1) product))))))
(define exp2
  (lambda (base n)
    (exp-iter base n 1)))

(exp2 2 10)

sicp在线版本

core war的一些笔记

core war的wiki页: core war
大学里看过这游戏, 忘得差不多了. 做一个备忘.

Mars的特点

1. 地址是循环的, 就是一个Ring.
    假设内存大小是8000的话,则第一个内存的地址是0,最后一个是7999,它的下一个又是0.

2.没有绝对地址,只有相对地址.
   当前指令的地址是0,下一个是1,前面一个单位的地址是-1.

3. 存储的最小单位不是Byte,而是指令.
  Mars里,所有指令的大小都是1.

4. Mars是多任务的
   Mars内部有一个pqueue (process queue).
   一个进程可以SPL(fork). eg. A战士有3个进程A1,A2,A3,了只有一个B1
  则在pqueue的排列是 A1 B1 A2 B1 A3 B1 ...

5. 传统的Mars初始时每一个内存都是DAT #0,#0

6.胜负条件是有一方所有进程退出.
   程序执行了非法指令(DAT)会退出.

Redcode的语法

label opr A field, B field.

寻址方式

1. 立即寻址 #.
MOV  #7, 1
将7(数字)移动到下一条指令的B field.

2. 直接寻址, $, ($可以省略)
MOV 0,  1
将本条指令(整个) copy到下一个位置

3. A Field 间接寻址*
   同B Field间接寻址.

4. B Field间接寻址 @
   DAT   #0, #4
   MOV  0,  @-1 (下一条要执行的语句)
   将当前指令copy到后面第4条指令的位置.

5. < -- B-field indirect with predecrement
   这个同间接寻址, 只是在这之前会先把算出来的地址减1, 同时本身减1. (没讲清楚,看下面的链接)


6. 还有3个与<类似的寻址方式
>,{,}

redcode的经典文章: http://vyznev.net/corewar/guide.html

Example

1. imp
最经典的战士,只有一行:

;name imp
;assert 1
MOV 0, 1

很好懂, 将自身copy到下一个指令, 如此循环下去. 没有攻击性,除非其它战士执行非法指令退出.
杀伤力为0.

2. Gate
专门针对上面说到的imp
;name gate
;strategy ...

gate EQU wait - 10
wait  JMP 0, <gate
END wait

EQU和END是宏指令, 不是真的redcode.
END表明程序从wait开始.
JMP 0, <gate的意思是程序一直跳到当前行, 同时将-10的B Field减1.
当imp来到gate时, 会被变成 MOV 0, 0 imp执行MOV 0,0后,会执行下一个非法的指令而退出.
MOV 0, 0
DAT  #0, #0

redcode集锦: http://www.koth.org/planar/by-date/idx.htm

指令集


DAT 存放数据用的.


MOV


ADD,SUB,MOD,MUL,DIV


JMP, JMN, JMZ,SPL