Definite's Extractor

My findings on Life, Linux, Open Source, and so on.

Why currying?

拜讀  Jserv 的 以 C 語言實做 Functional Language 的 Currying 後,又去 Wikipedia 找了找 Currying 介紹, 一開始的印象其實是:「要是只要綁參數,那用 Macro 就好了。這東西該不會是學術界的蛋頭們發明出來裝高深吧。」

後來一想,一堆人都在用,不會大家都裝高深吧。於是繼續探索,發現也有一些人和我有同樣的疑問: What is ‘Currying’?What are the practical advantages of currying? 

其中講的最清楚的,就是 What are the practical advantages of currying? 的第一個答案。

簡單的說呢,不用 currying, 要寫把多個參數值相加的函數,可能就得

add2(a,b)
add3(a,b,c)
add4(a,b.c)

有了  curry, 你只需要寫一個

(define add_c
  ((curry action) +))

用的時候可以v像這樣

add_c 2 3 4 5

當然啦,要玩 currying, 還是得程式語言本身支援比較方便。

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: