Jess Gabriel y Galn schreef op 9-6-2014 12:06:
> On Mon, Jun 9, 2014 at 11:09 AM, Roelof Wobben <r.wobben / home.nl> wrote:
>> Roelof Wobben schreef op 8-6-2014 22:39:
>>
>> Matthew Kerwin schreef op 8-6-2014 13:13:
>>
>> On 8 June 2014 18:57, Roelof Wobben <r.wobben / home.nl> wrote:
>>>
>>> Thanks,
>>>
>>> This works and I have studie this and the rest of this topic.
>>> If I understand everything right it works like this :
>>>
>>> one(add(one))
>>> function one will be executed.
>>> if fn = right
>>>    fn[1] will be add[1, one]
>>> add will  be executed
>>> (y)  { 1+ one}
>>> so one will be executed again.
>>> if fn = false
>>> 1
>>> (y) { 1+1}
>>> y = 2
>>>
>>> Do I understand everything well ?
>>>
>> Pretty much, but the execution order is the opposite. The inner-most
>> parameters have to be evaluated before they can be passed to the function,
>> so first is executed the inner `one`, then `add(...)`, then the outer
>> `one(...)`.
>>
>> --
>>    Matthew Kerwin
>>    http://matthew.kerwin.net.au/
>>
>>
>> Oke,
>> Thanks for the help, explantion andt the patience with me.
>> Still I find it a wierd order.
>>
>> Roelof
>>
>>
>>
>> Still a wierd problem.
>> I did make this with your example.
>>
>> def zero fn=nil
>>    if fn
>>      fn[0]
>>    else
>>      0
>>    end
>> end
>>
>> def one fn=nil
>>    if fn
>>      fn[1]
>>    else
>>      1
>>    end
>> end
>>
>> def two fn=nil
>>    if fn
>>      fn[2]
>>    else
>>      2
>>    end
>> end
>>
>> def three fn=nil
>>    if fn
>>      fn[3]
>>    else
>>      3
>>    end
>> end
>>
>> def four fn=nil
>>    if fn
>>      fn[4]
>>    else
>>      4
>>    end
>> end
>>
>> def five fn=nil
>>    if fn
>>      fn[5]
>>    else
>>      5
>>    end
>> end
>>
>> def six fn=nil
>>    if fn
>>      fn[6]
>>    else
>>      6
>>    end
>> end
>>
>> def seven fn=nil
>>    if fn
>>      fn[7]
>>    else
>>      7
>>    end
>> end
>>
>> def eight fn=nil
>>    if fn
>>      fn[8]
>>    else
>>      8
>>    end
>> end
>>
>> def nine fn=nil
>>    if fn
>>      fn[9]
>>    else
>>      9
>>    end
>> end
>>
>> def plus
>>    ->(y) { y + x }
>> end
>>
>> def minus
>>    ->(y) { y - x }
>> end
>>
>> def times
>>    ->(y) { y * x }
>> end
>>
>> def divided_by
>>    ->(y) { y / x }
>> end
>>
>> four(plus(nine))
>>
>> and now I see this error :
>>
>> main.rb:83:in `plus': wrong number of arguments (1 for 0) (ArgumentError)
>> from main.rb:99:in ` '
>> Roelof
>>
>>
> Your methods need to receive an argument:
>
> def plus x
>    ->(y) {y + x}
> end
>
> The argument they receive is the one that is curried:
>
> 2.0.0p195 :023 > %w{one two three four five six seven eight
> nine}.each_with_index do |x, i|
> 2.0.0p195 :024 >     define_method(x) do |fn=nil|
> 2.0.0p195 :025 >       fn.nil? ? i+1 : fn[i+1]
> 2.0.0p195 :026?>     end
> 2.0.0p195 :027?>   end
>
> 2.0.0p195 :030 > def plus x
> 2.0.0p195 :031?>     ->(y) {y + x}
> 2.0.0p195 :032?>   end
>   => nil
> 2.0.0p195 :033 > one(plus(one))
>   => 2
> 2.0.0p195 :034 > def minus x
> 2.0.0p195 :035?>   ->(y) {y - x}
> 2.0.0p195 :036?>   end
>   => nil
> 2.0.0p195 :037 > def times x
> 2.0.0p195 :038?>   ->(y) {y * x}
> 2.0.0p195 :039?>   end
>
> 2.0.0p195 :042 > three(times(one(plus(one))))
>   => 6
>
> Jesus.
>
Thanks, still I wonder why on the simple case the x is not needed .

Roelof