hemanth / functional-programming-jargon

Jargon from the functional programming world in simple terms!
http://git.io/fp-jargons
MIT License
18.59k stars 1.02k forks source link

Add Combinator Definition #126

Closed amilajack closed 2 years ago

jethrolarson commented 7 years ago

You know what you'd like it to say?

hemanth commented 7 years ago

Are you referring to Fixed-point combinator?

jethrolarson commented 7 years ago

There's a whole bunch of them. S, K, B, C, I, W, and everything in aviary...

On Sun, Dec 11, 2016, 9:06 PM hemanth.hm notifications@github.com wrote:

Are you referring to Fixed-point combinator?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/hemanth/functional-programming-jargon/issues/126#issuecomment-266344286, or mute the thread https://github.com/notifications/unsubscribe-auth/AAB-4BRO5yCr5GIamUm-AriEqYdBUlnvks5rHNZagaJpZM4LBzX4 .

hemanth commented 7 years ago

And then there is Y 🤗

On Mon, Dec 12, 2016, 4:46 PM Jethro Larson notifications@github.com wrote:

There's a whole bunch of them. S, K, B, C, I, W, and everything in aviary...

On Sun, Dec 11, 2016, 9:06 PM hemanth.hm notifications@github.com wrote:

Are you referring to Fixed-point combinator?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub < https://github.com/hemanth/functional-programming-jargon/issues/126#issuecomment-266344286 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AAB-4BRO5yCr5GIamUm-AriEqYdBUlnvks5rHNZagaJpZM4LBzX4

.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/hemanth/functional-programming-jargon/issues/126#issuecomment-266403110, or mute the thread https://github.com/notifications/unsubscribe-auth/AABHi06wKI1GHyA5ZqTYktDOgitLh1Nqks5rHSzxgaJpZM4LBzX4 .

stereobooster commented 7 years ago

Most known fixed-point combinator is Y combinator, but it will not work in JS, because of order of evaluation. So there is Z combinator to the rescue

// The Y combinator is defined as:
// Y          = λf.(λx.f (x x))(λx.f (x x))
// The Z combinator is defined as:
// Z          = λf.(λx.f (λv.((x x) v))) (λx.f (λv.((x x) v)))

const Z       = f => (x => f (v => ((x (x)) (v)))) (x => f (v => ((x (x)) (v))))

// example of usage: factorial
const factgen = fact => n => n < 2 ? 1 : n * fact(n-1)
const fact    = Z(factgen)

Fixed-point combinator is a function to find fixed point of other function. Simple example of fixed point of function is dotty number e.g. solution of cos(x) = x.

But the most striking thing about fixed-point combinator if it is applied to higher order function e.g. function which returns function, fixed point of higher order function is function too. First time I realized it blowed my mind.

stereobooster commented 7 years ago

Other idea: we can expand Lambda Calculus section.

A lot of simple Lambda Calculus ideas pretty trivial to demonstrate in JS

//True   = λt.λf.t
//False  = λt.λf.f
//Not    = λb.b False True

const True   = t => f => t
const False  = t => f => f
const Not    = b => b (False) (True)

const toBool = (churchBoolean) => churchBoolean (true) (false)

//Zero   = λf.λx.x
//Succ   = λn.λf.λx.f (n f x)

const Zero   = f => z => z
const Succ   = n => f => z => f (n (f) (z))

const toNumber = (churchNumeral) => churchNumeral((x) => x+1) (0)

But I suppose it can be separate page dedicated to Lambda Calculus.

jethrolarson commented 7 years ago

Church encoding is cool but I think it's too far away from being practical to try to teach it here

On Thu, Dec 22, 2016, 4:41 PM stereobooster notifications@github.com wrote:

Other idea: we can expand Lambda Calculus section.

A lot of simple Lambda Calculus ideas pretty trivial to demonstrate in JS

//True = λt.λf.t//False = λt.λf.f//Not = λb.b False True const True = t => f => tconst False = t => f => fconst Not = b => b (False) (True) const toBool = (churchBoolean) => churchBoolean (true) (false) //Zero = λf.λx.x//Succ = λn.λf.λx.f (n f x) const Zero = f => z => zconst Succ = n => f => z => f (n (f) (z))const toNumber = (churchNumeral) => churchNumeral((x) => x+1) (0)

But I suppose it can be separate page dedicated to Lambda Calculus.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/hemanth/functional-programming-jargon/issues/126#issuecomment-268921689, or mute the thread https://github.com/notifications/unsubscribe-auth/AAB-4Ihd_J1-MxTNDvfW9Lm85WAusGvbks5rKxjEgaJpZM4LBzX4 .

SeanSilke commented 7 years ago

It would be nice to see definition of various combinations. The C combinator is rather practical and can be used in everyday coding.

jethrolarson commented 7 years ago

Would the combinators all be curried?

On Thu, Jan 19, 2017, 1:57 AM Sergey Orlov notifications@github.com wrote:

It would be nice to see definition of various combinations. The C combinator is rather practical and can be used in everyday coding.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/hemanth/functional-programming-jargon/issues/126#issuecomment-273729590, or mute the thread https://github.com/notifications/unsubscribe-auth/AAB-4HFX1LEJ9NgEs-PymHn4c53KbRMNks5rTzOGgaJpZM4LBzX4 .

SeanSilke commented 7 years ago

@jethrolarson Don't sure that i get your question right. Yes C combinator is type of curing. My knowledge of combinators is rather limited, actually i get it from this great talk of Reginald Braithwaite https://youtu.be/3t75HPU2c44

jethrolarson commented 7 years ago

I think if we have functional combinators in the doc we should just have a general definition and not try to teach the details of every combinator. We can link out for that.

stereobooster commented 7 years ago

Also worth to elaborate a bit more, about why Y combinator does not work. It is because order of evaluation and add link to Lazy evaluation section

jethrolarson commented 2 years ago

Here's a whack at it:

Functional Combinator

A higher-order function, usually curried, which returns a new function changed in some way. Functional combinators are often used in point-free programming to write especially terse programs.