心靈與程式碼的協奏曲
by thinker
最後更新時間: 2011-04-14 00:27:20 CST | 引用
查詢:
COMMENTS:
on 2011-04-14 10:24:49 CST
reborn2266 said ..
跟literate programming的作法很類似,但LP可讓前後順序的撰寫更隨性。
on 2011-04-14 10:37:38 CST
Thinker said ..
LP 確實有類似的功能,但目的不同。LP 著重 code block 的組織,而本文所解決的是程式碼推論和寫下的一致性。本文採用的是由後往前寫的方式。事實上,在使用這個方法之前,原先的想法是使用 lambda ,讓 evaluation 往後延,以提供 forward reference 的效果。lambda 的方式就能達到任意順序的目的。lambda 的方法請參考 http://nopaste.me/paste/4d9f207414a29
on 2011-04-30 17:30:41 CST
drake said ..
配合 vim 的 O 來往回寫,還算挺有趣的一個經驗吶~
on 2012-01-09 01:58:37 CST
said ..
用 Haskell 的 where,可以從上到下,逆序書寫: mul_n_sum :: String -> Int mul_n_sum str = result where result = sum value_of_lines value_of_lines = [ field1 * field2 | [field1, field2] <- field_lines ] field_lines = [ [read txt1, read txt2] | [txt1, txt2] <- txt_lines ] txt_lines = [ words line | line <- lines str ]
on 2012-01-09 02:00:11 CST
said ..
修正格式。 {{{#!haskell mul_n_sum :: String -> Int mul_n_sum str = result where result = sum value_of_lines value_of_lines = [ field1 * field2 | [field1, field2] <- field_lines ] field_lines = [ [read txt1, read txt2] | [txt1, txt2] <- txt_lines ] txt_lines = [ words line | line <- lines str ] }}}
on 2012-01-09 02:01:17 CST
said ..
再試一次格式... 源碼在 https://gist.github.com/1579130 。 {{{#!python mul_n_sum :: String -> Int mul_n_sum str = result where result = sum value_of_lines value_of_lines = [ field1 * field2 | [field1, field2] <- field_lines ] field_lines = [ [read txt1, read txt2] | [txt1, txt2] <- txt_lines ] txt_lines = [ words line | line <- lines str ] }}}
on 2012-01-09 09:52:15 CST
Thinker said ..
很抱歉,我會找時間改善留言系統。 對於 functional programming language 而言,確實天生就支援這樣的 coding 方式。 而 python 可以使用 lambda 完成。