2013-06-04 40 views

回答

6

聲明式格式是您聲明程序的意圖/最終結果,而不是它應該如何到達的格式。本質上它是一個配置文件和一段代碼之間的區別。比較:

CC=gcc 
CFLAGS=-I. 
DEPS = hellomake.h 

%.o: %.c $(DEPS) 
    $(CC) -c -o [email protected] $< $(CFLAGS) 

hellomake: hellomake.o hellofunc.o 
    gcc -o hellomake hellomake.o hellofunc.o -I. 

對像(上python-僞代碼):

CC = "gcc" 
CFLAGS = "-I." 
DEPS = ["hellomake.h"] 
def make_o_file(o_file, deps): 
    #don't recompile if it exists & is current 
    c_file = "%s.c" % os.path.splitext(o_file)[0] 
    if (os.path.exists(o_file) 
      and is_newer(o_file, c_file) 
      and all(is_newer(o_file, dep) for dep in deps)): 
     return 

    #else actually compile it 
    compile_c_file(CC, code_file, o_file, CFLAGS) 

if target == "hellomake": 
    make_o_file("hellomake.o", DEPS) 
    make_o_file("hellofunc.o", DEPS) 
    link_o_files("hellomake", ["hellomake.o", "hellofunc.o"]) 

前者可以更容易爲人類的過程,如果格式是精心設計的。關於declarative programming的維基百科可能會有用。