Re: Lambda in parameters

2020-12-19 Thread Abdur-Rahmaan Janhangeer
Greetings list,


Jane Street is well known for its love of functional programming in general
and of OCaml in particular.  If you don't know OCaml yet, I highly
recommend it.  You can think of it as Python with (static) types.

Yes i know OCaml when i was exploring haskell and the like.
At that time i was looking for companies who use OCaml etc
Did not come across Jane Street (If you know someone from there
tell them the question writer has a really twisted mind ^^)/.
I don't understand why though you would prefer OCaml over Python, Go, Java
etc for a company ^^

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Lambda in parameters

2020-12-19 Thread Abdur-Rahmaan Janhangeer
Greetings list,


 Why car and cdr?

Well obviously car is content of the address register and cdr is content of
data register.
Apparently an artefact of a early implementation of lisp.

Oh did not know that detail. More twists for sure. Thought lisp did not go
lowlevel.

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Lambda in parameters

2020-12-19 Thread Abdur-Rahmaan Janhangeer
Greetings,

Very clear explanations, rewriting lambdas as a function
was the key to understand it. Did not know was a lisp inspiration.

My mind still spiralling XD

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Lambda in parameters

2020-12-19 Thread Greg Ewing

On 18/12/20 7:02 pm, Cameron Simpson wrote:

Frankly, I think this is a terrible way to solve this problem, whatever
the problem was supposed to be - that is not clear.


It demonstrates that a programming language doesn't strictly
need data structes -- you can do everything with nothing
but functions.

This is mainly of theoretical interest; it's not usually a
practical way to go about things. But it's a good exercise
in thinking about functions as objects to be manipulated.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Lambda in parameters

2020-12-19 Thread Cameron Simpson
On 19Dec2020 07:39, Philippe Meunier  wrote:
>See also the example reduction
>here: https://en.wikipedia.org/wiki/Church_encoding#Church_pairs

Thank you for this reference. I've stuck it on my reading list.

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Lambda in parameters

2020-12-19 Thread Philippe Meunier
Abdur-Rahmaan Janhangeer wrote:
>The aim of car is to return 1
>but i don't understand how lambda achieves this

Cameron Simpson's explanation is very good.  See also the example reduction
here: https://en.wikipedia.org/wiki/Church_encoding#Church_pairs

>This problem was asked by Jane Street.

Jane Street is well known for its love of functional programming in general
and of OCaml in particular.  If you don't know OCaml yet, I highly
recommend it.  You can think of it as Python with (static) types.

Best,

Philippe


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Lambda in parameters

2020-12-18 Thread Grant Edwards
On 2020-12-18, Barry  wrote:
>> Implement car and cdr.
>  Why car and cdr?
>
> Well obviously car is content of the address register and cdr is content of 
> data register.
> Apparently an artefact of a early implementation of lisp.

While car and cdr are lisp operators, the "content of address
register" and "content of data register" etymology is apparently
apocryphal:

  https://en.wikipedia.org/wiki/CAR_and_CDR#Etymology

--
Grant

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Lambda in parameters

2020-12-18 Thread Julio Di Egidio
On Friday, 18 December 2020 at 15:20:59 UTC+1, Abdur-Rahmaan Janhangeer wrote:
> The Question: 
> 
> # --- 
> This problem was asked by Jane Street. 
> 
> cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first 
> and last element of that pair. For example, car(cons(3, 4)) returns 3, and 
> cdr(cons(3, 4)) returns 4. 
> 
> Given this implementation of cons:
> def cons(a, b): 
> def pair(f): 
> return f(a, b) 
> return pair
> Implement car and cdr. 
> # --- 

Notice that you don't need (Python) lambdas to code it, plain function 
definitions are fine:

# ---
def cons(a, b):
def pair(f):
return f(a, b)
return pair

def car(pair):
def left(a, b):
return a
return pair(left)

pair = cons(1, 2)
assert car(pair) == 1
# ---

That said, few basic comments:  In Python, that 'cons' does not construct a 
pair, it rather returns a function with values a and b in its closure that, 
given some function, applies it to those values.  In fact, Python has tuples 
built-in, how to build them as well as how to access their members.  I take it 
the point of the exercise is how to use a purely functional language, such as 
here a fragment of Python, to encode (i.e. formalize) pairs and their 
operations.

Julio
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Lambda in parameters

2020-12-18 Thread Barry


> On 18 Dec 2020, at 14:23, Abdur-Rahmaan Janhangeer  
> wrote:
> 
> The Question:
> 
> # ---
> This problem was asked by Jane Street.
> 
> cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first
> and last element of that pair. For example, car(cons(3, 4)) returns 3, and
> cdr(cons(3, 4)) returns 4.
> 
> Given this implementation of cons:
> 
> def cons(a, b):
>def pair(f):
>return f(a, b)
>return pair
> 
> Implement car and cdr.
 Why car and cdr?

Well obviously car is content of the address register and cdr is content of 
data register.
Apparently an artefact of a early implementation of lisp.

Barry

> # ---
> 
> Kind Regards,
> 
> 
> Abdur-Rahmaan Janhangeer
> 
> https://www.github.com/Abdur-RahmaanJ
> 
> Mauritius
> 
> sent from gmail client on Android, that's why the signature is so ugly.
> 
>> On Fri, 18 Dec 2020, 10:02 Cameron Simpson,  wrote:
>> 
>>> On 17Dec2020 23:52, Abdur-Rahmaan Janhangeer  wrote:
>>> Here is a famous question's solution
>> 
>> These look like implementations of Lisp operators, which I've never
>> found easy to remember. So I'll do this from first principles, but
>> looking at the (uncommented) code.
>> 
>> This is some gratuitiously tricky code. Maybe that's needed for these
>> operators. But there's a lot here, so let's unpick it:
>> 
>>> def cons(a, b):
>>>   def pair(f):
>>>   return f(a, b)
>>>   return pair
>> 
>> Cons returns "pair", a function which itself accepts a function "f",
>> calls it with 2 values "a, b" and returns the result. For ultra
>> trickiness, those 2 values "a, b" are the ones you passed to the initial
>> call to cons().
>> 
>> This last bit is a closure: when you define a function, any nonlocal
>> variables (those you use but never assign to) have the defining scope
>> available for finding them. So the "pair" function gets "a" and "b" from
>> those you passed to "cons".
>> 
>> Anyway, cons() returns a "pair" function hooked to the "a" and "b" you
>> called it with.
>> 
>>> def car(c):
>>>   return c(lambda a, b: a)
>> 
>> The function accepts a function, and calls that function with "lambda a,
>> b: a", which is itself a function which returns its first argument. You
>> could write car like this:
>> 
>>def car(c):
>>def first(a, b):
>>return a
>>return c(first)
>> 
>> The lambda is just a way to write a simple single expression function.
>> 
>>> print(cons(1, 2)(lambda a, b: a))
>> 
>> What is "cons(1,2)". That returns a "pair" function hooked up to the a=1
>> and b=2 values you supplied. And the pair function accepts a function of
>> 2 variables.
>> 
>> What does this do?
>> 
>>cons(1, 2)(lambda a, b: a)
>> 
>> This takes the function returns by cons(1,2) and _calls_ that with a
>> simple function which accepts 2 values and returns the first of them.
>> 
>> So:
>> 
>>cons(1,2) => "pair function hooked to a=1 and b=2"
>> 
>> Then call:
>> 
>>pair(lambda a, b: a)
>> 
>> which sets "f" to the lambda function, can calls that with (a,b). So it
>> calls the lambda function with (1,2). Which returns 1.
>> 
>>> print(car(cons(1, 2)))
>> 
>> The "car" function pretty much just embodies the call-with-the-lambda.
>> 
>>> but i don't understand how lambda achieves this
>> 
>> If you rewite the lambda like this:
>> 
>>def a_from_ab(a,b):
>>return a
>> 
>> and then rewrite the first call to cons() like this:
>> 
>>cons(1,2)(a_from_ab)
>> 
>> does it make any more sense?
>> 
>> Frankly, I think this is a terrible way to solve this problem, whatever
>> the problem was supposed to be - that is not clear.
>> 
>> On the other hand, I presume it does implement the Lisp cons and car
>> functions. I truly have no idea, I just remember these names from my
>> brief brush with Lisp in the past.
>> 
>> Cheers,
>> Cameron Simpson 
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Lambda in parameters

2020-12-18 Thread Abdur-Rahmaan Janhangeer
The Question:

# ---
This problem was asked by Jane Street.

cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first
and last element of that pair. For example, car(cons(3, 4)) returns 3, and
cdr(cons(3, 4)) returns 4.

Given this implementation of cons:

def cons(a, b):
def pair(f):
return f(a, b)
return pair

Implement car and cdr.
# ---

Kind Regards,


Abdur-Rahmaan Janhangeer

https://www.github.com/Abdur-RahmaanJ

Mauritius

sent from gmail client on Android, that's why the signature is so ugly.

On Fri, 18 Dec 2020, 10:02 Cameron Simpson,  wrote:

> On 17Dec2020 23:52, Abdur-Rahmaan Janhangeer  wrote:
> >Here is a famous question's solution
>
> These look like implementations of Lisp operators, which I've never
> found easy to remember. So I'll do this from first principles, but
> looking at the (uncommented) code.
>
> This is some gratuitiously tricky code. Maybe that's needed for these
> operators. But there's a lot here, so let's unpick it:
>
> >def cons(a, b):
> >def pair(f):
> >return f(a, b)
> >return pair
>
> Cons returns "pair", a function which itself accepts a function "f",
> calls it with 2 values "a, b" and returns the result. For ultra
> trickiness, those 2 values "a, b" are the ones you passed to the initial
> call to cons().
>
> This last bit is a closure: when you define a function, any nonlocal
> variables (those you use but never assign to) have the defining scope
> available for finding them. So the "pair" function gets "a" and "b" from
> those you passed to "cons".
>
> Anyway, cons() returns a "pair" function hooked to the "a" and "b" you
> called it with.
>
> >def car(c):
> >return c(lambda a, b: a)
>
> The function accepts a function, and calls that function with "lambda a,
> b: a", which is itself a function which returns its first argument. You
> could write car like this:
>
> def car(c):
> def first(a, b):
> return a
> return c(first)
>
> The lambda is just a way to write a simple single expression function.
>
> >print(cons(1, 2)(lambda a, b: a))
>
> What is "cons(1,2)". That returns a "pair" function hooked up to the a=1
> and b=2 values you supplied. And the pair function accepts a function of
> 2 variables.
>
> What does this do?
>
> cons(1, 2)(lambda a, b: a)
>
> This takes the function returns by cons(1,2) and _calls_ that with a
> simple function which accepts 2 values and returns the first of them.
>
> So:
>
> cons(1,2) => "pair function hooked to a=1 and b=2"
>
> Then call:
>
> pair(lambda a, b: a)
>
> which sets "f" to the lambda function, can calls that with (a,b). So it
> calls the lambda function with (1,2). Which returns 1.
>
> >print(car(cons(1, 2)))
>
> The "car" function pretty much just embodies the call-with-the-lambda.
>
> >but i don't understand how lambda achieves this
>
> If you rewite the lambda like this:
>
> def a_from_ab(a,b):
> return a
>
> and then rewrite the first call to cons() like this:
>
> cons(1,2)(a_from_ab)
>
> does it make any more sense?
>
> Frankly, I think this is a terrible way to solve this problem, whatever
> the problem was supposed to be - that is not clear.
>
> On the other hand, I presume it does implement the Lisp cons and car
> functions. I truly have no idea, I just remember these names from my
> brief brush with Lisp in the past.
>
> Cheers,
> Cameron Simpson 
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Lambda in parameters

2020-12-17 Thread Cameron Simpson
On 17Dec2020 23:52, Abdur-Rahmaan Janhangeer  wrote:
>Here is a famous question's solution

These look like implementations of Lisp operators, which I've never 
found easy to remember. So I'll do this from first principles, but 
looking at the (uncommented) code.

This is some gratuitiously tricky code. Maybe that's needed for these 
operators. But there's a lot here, so let's unpick it:

>def cons(a, b):
>def pair(f):
>return f(a, b)
>return pair

Cons returns "pair", a function which itself accepts a function "f", 
calls it with 2 values "a, b" and returns the result. For ultra 
trickiness, those 2 values "a, b" are the ones you passed to the initial 
call to cons().

This last bit is a closure: when you define a function, any nonlocal 
variables (those you use but never assign to) have the defining scope 
available for finding them. So the "pair" function gets "a" and "b" from 
those you passed to "cons".

Anyway, cons() returns a "pair" function hooked to the "a" and "b" you 
called it with.

>def car(c):
>return c(lambda a, b: a)

The function accepts a function, and calls that function with "lambda a, 
b: a", which is itself a function which returns its first argument. You 
could write car like this:

def car(c):
def first(a, b):
return a
return c(first)

The lambda is just a way to write a simple single expression function.

>print(cons(1, 2)(lambda a, b: a))

What is "cons(1,2)". That returns a "pair" function hooked up to the a=1 
and b=2 values you supplied. And the pair function accepts a function of 
2 variables.

What does this do?

cons(1, 2)(lambda a, b: a)

This takes the function returns by cons(1,2) and _calls_ that with a 
simple function which accepts 2 values and returns the first of them.

So:

cons(1,2) => "pair function hooked to a=1 and b=2"

Then call:

pair(lambda a, b: a)

which sets "f" to the lambda function, can calls that with (a,b). So it 
calls the lambda function with (1,2). Which returns 1.

>print(car(cons(1, 2)))

The "car" function pretty much just embodies the call-with-the-lambda.

>but i don't understand how lambda achieves this

If you rewite the lambda like this:

def a_from_ab(a,b):
return a

and then rewrite the first call to cons() like this:

cons(1,2)(a_from_ab)

does it make any more sense?

Frankly, I think this is a terrible way to solve this problem, whatever 
the problem was supposed to be - that is not clear.

On the other hand, I presume it does implement the Lisp cons and car 
functions. I truly have no idea, I just remember these names from my 
brief brush with Lisp in the past.

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list