[Jprogramming] Revisisting the Y combinator

'Mike Day' via Programming programming at jsoftware.com
Tue Nov 13 23:59:09 UTC 2018


My effort to make it fully tacit results in this rather verbose form:
  tfib =: {.[: {."1[:([: +/\ |.)^:([:i.[)&0 1(+ [:-.2|])
  tfib 10
0 1 1 2 3 5 8 13 21 34
  tfib 11
0 1 1 2 3 5 8 13 21 34 55
This is shorter, but only works correctly for odd arguments
  tfibodd =: {.[: {."1([: +/\ |.)^:([:i.[)&0 1
  tfibodd 11
0 1 1 2 3 5 8 13 21 34 55
  tfibodd 10
0 0 0 0 0 0 0 0 0 0
Probably better to stick to explicit!
NB. There are other approaches, especially for large inputs.
Mike
On 13/11/2018 17:46, Linda Alvord wrote:
> Mike, that was great!!
>> I managed to deal with odds and evens.
>> However, because of [: which I can't seem to remove, it won't turn tacit.
>> It is almost "high school ready"
>> flla=: 13 :'1{"1([:+/\|.)^:(<:i.y)1 1'
> flla 10
> 1 1 2 3 5 8 13 21 34 55
> flla 11
> 1 1 2 3 5 8 13 21 34 55 89
> flla
> ┌─┬─┬─────────────────────────┐
> │3│:│1{"1([:+/\|.)^:(<:i.y)1 1│
> └─┴─┴─────────────────────────┘
>> Linda
>> -----Original Message-----
> From: Programming <programming-bounces at forums.jsoftware.com> On Behalf Of 'Mike Day' via Programming
> Sent: Monday, November 12, 2018 1:59 PM
> To: programming at jsoftware.com
> Subject: Re: [Jprogramming] Revisisting the Y combinator
>> NB. I've omitted Jose's rather lengthy original posting to save space.
>> Re Linda's quoted Fibonacci verb,
>> 13 :',(([:+/\|.)^:2)^:(<`(1,1:))y'
>> I couldn't understand it until I checked the vocabulary entry for ^:
>> a) u ^: (<n) does the same as u ^: (i.n)
>> b) u ^: (v1`v2) y does u^:(v1 y) (v2 y)
>> The engine of this fibonacci sequence generator is
>>  ([:+/\|.)
>> as in
>>  ([:+/\|.) 1 1   NB. f2 f3 given f1 f2 = 1 1
> 1 2
>> The rest is controlling the repetition of the engine's application.
>> So, developing the sequence,
>>  ([:+/\|.)^:(0 1 2 3 4) 1 1  NB. generate pairs f2 f3, f3 f4, ... ,f5 f6
> 1 1
> 1 2
> 2 3
> 3 5
> 5 8
>>  ([:+/\|.)^:2^:(0 1 2) 1 1  NB. only generate "even" pairs
> 1 1
> 2 3
> 5 8
>>  ([:+/\|.)^:2^:(<3) 1 1   NB. using ^: property (a), as above,
> 1 1
> 2 3
> 5 8
>>  ([:+/\|.)^:2^:(<`(1,1:)) 3   NB. gerund form of power, see (b) above
> 1 1
> 2 3
> 5 8
>>  ,([:+/\|.)^:2^:(<`(1,1:)) 3 NB. ravel result
> 1 1 2 3 5 8
>> I can't get my head round the Y combinator, but this isn't an example!
>> Linda wondered why it stopped at 34 or 89 but not 55.
> By its construction , this Fibonacci only does even numbers, yielding elements 1 to 2*y
>> We could start from f0 f1, ie 0 1:
>>  ,([:+/\|.)^:2^:(<`(0,1:)) 3  NB. odd-number version
> 0 1 1 2 3 5
>> Cheers,
>> Mike
>>> On 12/11/2018 08:20, Linda Alvord wrote:
>> Sorry about the post in the wrong thread.
>>>> There should be some "Y combinator" at work in here.
>>>> f=: 13 :',([:+/\|.)^:2^:(<`(0,1:))y'
>>>>>> f 5
>> 0 1 1 2 3 5 8 13 21 34
>> f 6
>> 0 1 1 2 3 5 8 13 21 34 55 89
>>>> I can't seem to stop at 55. Any ideas?
>>>> Linda
>>>> -----Original Message-----
>> From: Programming <programming-bounces at forums.jsoftware.com> On Behalf
>> Of Linda Alvord
>> Sent: Friday, November 9, 2018 12:56 AM
>> To: programming at jsoftware.com
>> Subject: Re: [Jprogramming] Revisisting the Y combinator
>>>> Jose, I'm not sure I'll be able to follow your ideas.
>>>> !i.11x
>> 1 1 2 6 24 120 720 5040 40320 362880 3628800
>>>>>> However, Fibohacci would be a nice primitive.
>>>> Linda
>>>> -----Original Message-----
>> From: Programming <programming-bounces at forums.jsoftware.com> On Behalf
>> Of 'Pascal Jasmin' via Programming
>> Sent: Thursday, November 8, 2018 10:41 PM
>> To: programming at jsoftware.com
>> Subject: Re: [Jprogramming] Revisisting the Y combinator
>>>>>>>>>> Interesting, thank you Jose.
>>>> I'll note that if the argument to Y is not an ar of an adverb then J (806 and 807) will crash when the result verb is called.
>>>>>> ________________________________
>> From: Jose Mario Quintana <jose.mario.quintana at gmail.com>
>>>> ---
> This email has been checked for viruses by Avast antivirus software.
> https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.avast.com%2Fantivirus&data=02%7C01%7C%7C70903383dc2f4151efd808d648d0decc%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636776459254645130&sdata=mP7rC7yN%2BuJS4qiMVFghLinRoT3ekbmvO7oRI4xHtqc%3D&reserved=0
>> ----------------------------------------------------------------------
> For information about J forums see https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.jsoftware.com%2Fforums.htm&data=02%7C01%7C%7C70903383dc2f4151efd808d648d0decc%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636776459254645130&sdata=6k9%2FprHwUzkgNvyoCY0efJMeeQb4x3EJogOrQXXXCJk%3D&reserved=0
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus


More information about the Programming mailing list

AltStyle によって変換されたページ (->オリジナル) /