Talk:Anonymous recursion: Difference between revisions

Content added Content deleted
No edit summary
(J Example)
Line 114: Line 114:


:The J solution is incorrect because it does not check for a negative sign, it simply implements the fibonacci function. Using $: is problematic because it is a reference to the largest verb which means that it will re-execute the entire function including the check if such a check is included. I have fixed the Mathematica example so that the check is performed only once, #0 is local to the anonymous function it is referenced in, this is the fibonacci function and not the function with the check. --[[User:Zeotrope|Zeotrope]] 16:18, 8 January 2011 (UTC)
:The J solution is incorrect because it does not check for a negative sign, it simply implements the fibonacci function. Using $: is problematic because it is a reference to the largest verb which means that it will re-execute the entire function including the check if such a check is included. I have fixed the Mathematica example so that the check is performed only once, #0 is local to the anonymous function it is referenced in, this is the fibonacci function and not the function with the check. --[[User:Zeotrope|Zeotrope]] 16:18, 8 January 2011 (UTC)

::Since the J solution acts like an identity when called with negative arguments the check for negative numbers can be performed after the fibonacci function is called, signalling a domain error if the result is negative: <lang J>fibN =: ([: [:^:(0&>) (-&2 +&$: -&1)^:(1&<) M.)"0</lang> --[[User:Zeotrope|Zeotrope]] 16:37, 8 January 2011 (UTC)