# What is the right way to make 1 from the string "one"

Hello,

Im trying to understand currying.
I understand how you can add, substract two numbers by using currying.

But I want to make it a step harder.

How can I translate the word “one” by the number 1 so I can use it on a

I tried :

def one 1
Proc.new do |x|
x = 1
end
end

but that one does not give the 1 back.

Roelof

On 7 June 2014 17:48, Roelof W. [email protected] wrote:

Hello,

Im trying to understand currying.
I understand how you can add, substract two numbers by using currying.

​I think maybe you don’t actually understand what currying is.

Currying basically​ means: taking a function that takes N arguments, and
turning it into a nested sequence of N functions that each take 1
argument.
The practical use is “partial application”, where you assign values to
some
of the functions.

For example, take this lambda function:

mean = ->(a, b, c) do
(a + b + c) / 3
end
mean[1, 2, 6] # => 3

​It takes three parameters, and returns their geometric mean. It doesn’t
make sense to call it with only one parameter.

If I curry it, I get a new lambda function which takes one parameter,
and
returns a new lambda function which remembers that parameter, and
accepts
the next parameter, etc.:

curried = mean.curry # ~= mean(?, ?, ?)
mean_1 = curried[1] # ~= mean(1, ?, ?)
mean_1_2 = mean_1[2] # ~= mean(1, 2, ?)
mean_1_2[6] # ~= mean(1, 2, 3) => 3
​ mean_1_2[9] # ~= mean(1, 2, 9) => 4​
mean_1_3 = mean_1[3] # ~= mean(1, 3, ?)
mean_1_3[11] # ~= mean(1, 3, 11) => 5

But I want to make it a step harder.

end

but that one does not give the 1 back.

I can’t even work out the intent of that function, to give advice on how
to
make it work. Does this not do what you want?

def one
1
end
3 + one # => 4

​Cheers

On Saturday, June 07, 2014 11:37:32 AM Roelof W. wrote:

Roelof

your lines. All lines are wrapped withing HTML.

# Regards, Arup R.

Debugging is twice as hard as writing the code in the first place.
Therefore,
if you write the code as cleverly as possible, you are, by definition,
not
smart enough to debug it.

–Brian Kernighan

Matthew K. schreef op 7-6-2014 11:33:
On 7 June 2014 17:48, Roelof W. wrote:
Hello,

Im trying to understand currying.
I understand how you can add, substract two numbers by using currying.

???I think maybe you don't actually understand what currying is.

Currying basically??? means: taking a function that takes N arguments, and turning it into a nested sequence of N functions that each take 1 argument. The practical use is "partial application", where you assign values to some of the functions.

For example, take this lambda function:??

?? mean = ->(a, b, c) do
?? ?? (a + b + c) / 3
?? end
?? mean[1, 2, 6] # => 3

???It takes three parameters, and returns their geometric mean. It doesn't make sense to call it with only one parameter.

If I curry it, I get a new lambda function which takes one parameter, and returns a new lambda function which remembers that parameter, and accepts the next parameter, etc.:

?? curried = mean.curry??# ~= mean(?, ?, ?)
?? mean_1 = curried[1] ??# ~= mean(1, ?, ?)
?? mean_1_2 = mean_1[2] # ~= mean(1, 2, ?)
?? mean_1_2[6] ?? ?? ?? ?? ??# ~= mean(1, 2, 3) => 3
??? ??mean_1_2[9] ?? ?? ?? ?? ??# ~= mean(1, 2, 9) => 4???
?? mean_1_3 = mean_1[3] # ~= mean(1, 3, ?)
?? mean_1_3[11] ?? ?? ?? ?? # ~= mean(1, 3, 11) => 5

But I want to make it a step harder.

How can I ??translate the word "one" by the number 1 so I can use it on a add function.

I tried :

def one 1
?? ??Proc.new do ?? |x|
?? ?? ?? x = 1
?? ??end
end

but that one does not give the 1 back.

I can't even work out the intent of that function, to give advice on how to make it work. Does this not do what you want?

?? def one
?? ?? 1
?? end
?? 3 + one # => 4

??? Cheers
--

The exercise I try to do it this one

Suppose you have this

So I have two functions

def one
end

end

Roelof

On 7 June 2014 19:37, Roelof W. [email protected] wrote:

end

end

Ah, I begin to understand. I’d approach it from the other direction:

add = ->(a,b) { a + b }

one_plus[43] # => 44

Or, if you want to be obtuse:

one = 1
def one fn
fn[1]
end
->(y) { y + x }
end

However I’d argue that this is using a simple closure (the lambda
function
returned from #add) rather than currying. It also relies on the fact
that
one and one() are unambiguously a local variable and a function,
respectively; however if you were to use Ruby’s currying, you’d have a
proc
object rather than a function, and so couldn’t use the () syntax to
invoke
it, so the two definitions of `one` would be ambiguous.

Matthew K. schreef op 7-6-2014 12:06:
On 7 June 2014 19:37, Roelof W. wrote:

The exercise I try to do it this one

Suppose you have this

So I have two functions

def one
end

end

Ah, I begin to understand. I'd approach it from the other direction:

?? add = ->(a,b) { a + b }
????
?? one_plus[43] # => 44

Or, if you want to be obtuse:

?? one = 1
?? def one fn
?? ?? fn[1]
?? end
?? ?? ->(y) { y + x }
?? end
????

However I'd argue that this is using a simple closure (the lambda function returned from #add) rather than currying. It also relies on the fact that one and one() are unambiguously a local variable and a function, respectively; however if you were to use Ruby's currying, you'd have a proc object rather than a function, and so couldn't use the () syntax to invoke it, so the two definitions of `one` would be ambiguous.

--

oke, I already thoug you need a proc that why I tried

def one.
?? Proc.new do |x|
?????????? x = 1
???? end
end

But as I said this is not working.
And I think I need ruby currying to do the job.

Roelof

On Jun 8, 2014 3:13 AM, “Roelof W.” [email protected] wrote:

Currying is harder then I thought.

What you’re doing is not currying.

I gave you an example using currying, and an example that correctly

def one fn=nil
if fn
fn[1]
else
1
end
end
->(y) { y + x }
end

*I made this up off the top of my head, on my phone, in bed. It probably
works, though. #add is a partially applied, curried version of ´y + x´,
but
#one is just intentional obscurity

Roelof W. schreef op 7-6-2014 13:48:
Matthew K. schreef op 7-6-2014 12:06:
On 7 June 2014 19:37, Roelof W. wrote:

The exercise I try to do it this one

Suppose you have this

So I have two functions

def one
end

end

Ah, I begin to understand. I'd approach it from the other direction:

?? add = ->(a,b) { a + b }
????
?? one_plus[43] # => 44

Or, if you want to be obtuse:

?? one = 1
?? def one fn
?? ?? fn[1]
?? end
?? ?? ->(y) { y + x }
?? end
????

However
``````            I'd argue that this is using a simple closure (the
lambda function returned from #add) rather than
currying. It also relies on the fact that </span><font
face="courier new, monospace"
``````

color="#000000">one and
one() are
unambiguously a local variable and a function,
respectively; however if you were to use Ruby’s
currying, you’d have a proc object rather than a
function, and so couldn’t use the () syntax to invoke
it, so the two definitions of `one` would be
ambiguous.

oke, I already thoug you need a proc that why I tried

def one.

?? Proc.new do |x|

??? x = 1

??? end

end

But as I said this is not working.

And I think I need ruby currying to do the job.

Roelof

I think I know how to solve this.

Can I also yield to a specific block and can I save the outcome like
this : x = yield

Roelof

Matthew K. schreef op 8-6-2014 1:37:

On Jun 8, 2014 3:13 AM, "Roelof W." <[email protected]> wrote:
>
> Currying is harder then I thought.
>

What you're doing is not currying.

I gave you an example using currying, *and* an example that correctly evaluates ??one(add(one))??

?? def one fn=nil
?????? if fn
?????????? fn[1]
?????? else
?????????? 1
?????? end
?? end
?????? ->(y) { y + x }
?? end

*I made this up off the top of my head, on my phone, in bed. It probably works, though. #add is a partially applied, curried version of ??y + x??, but #one is just intentional obscurity

Thanks,

This works and I have studie this and the rest of this topic.
If I understand everything right it works like this :

function one will be executed.
if fn = right??
?? fn[1] will be add[1, one]
(y)?? { 1+ one}
so one will be executed again.
if fn = false
1
(y) { 1+1}
y = 2

Do I understand everything well ?

Roelof

On 8 June 2014 18:57, Roelof W. [email protected] wrote:

(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(...)`.​

Roelof W. schreef op 7-6-2014 14:07:
Roelof W. schreef op 7-6-2014 13:48:
Matthew K. schreef op 7-6-2014 12:06:
On 7 June 2014 19:37, Roelof W. wrote:

The exercise I try to do it this one

Suppose you have this

So I have two functions

def one
end

end

Ah, I begin to understand. I'd approach it from the other direction:

?? add = ->(a,b) { a + b }
????
?? one_plus[43] # => 44

Or, if you want to be obtuse:

?? one = 1
?? def one fn
?? ?? fn[1]
?? end
?? ?? ->(y) { y + x }
?? end
????

However
``````              I'd argue that this is using a simple closure (the
lambda function returned from #add) rather than
currying. It also relies on the fact that </span><font
face="courier new, monospace"
``````

color="#000000">one and
one() are
unambiguously a local variable and a function,
respectively; however if you were to use Ruby’s
currying, you’d have a proc object rather than a
function, and so couldn’t use the () syntax to invoke
it, so the two definitions of `one` would be
ambiguous.

oke, I already thoug you need a proc that why I tried

def one.

?? Proc.new do |x|

??? x = 1

??? end

end

But as I said this is not working.

And I think I need ruby currying to do the job.

Roelof

I think I know how to solve this.

Can I also yield to a specific block and can I save the outcome
like this : x = yield

Roelof

Chips, still not working.

When I try this :

??

def one

??? 1

end

??Proc.new do |x,y|

??? y + x

?? end

end

I see these errors :

main.rb:8: warning: shadowing outer local variable - x
main.rb:8: warning: shadowing outer local variable - y
main.rb:7:in `add': wrong number of arguments (1 for 2) (ArgumentError) from main.rb:14:in`

Currying is harder then I thought.

Roelof

Matthew K. schreef op 8-6-2014 13:13:
On 8 June 2014 18:57, Roelof W. wrote:

Thanks,

This works and I have studie this and the rest of this topic.
If I understand everything right it works like this :

function one will be executed.
if fn = right??
?? fn[1] will be add[1, one]
(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(...)`.???

--

Oke,
Thanks for the help, explantion andt the patience with me.
Still I find it a wierd order.

Roelof

On Mon, Jun 9, 2014 at 11:09 AM, Roelof W. [email protected] wrote:

If I understand everything right it works like this :
(y) { 1+1}

end
if fn
3

``````fn[6]
``````

end
def nine fn=nil

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

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.

Jes??s Gabriel y Gal??n schreef op 9-6-2014 12:06:

If I understand everything right it works like this :
(y) { 1+1}
Matthew K.

`````` fn[2]
``````

end
def five fn=nil
else
end
if fn
def minus

2.0.0p195 :026?> end
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

Roelof W. schreef op 8-6-2014 22:39:
Matthew K. schreef op 8-6-2014 13:13:
On 8 June 2014 18:57, Roelof W. wrote:

Thanks,

This works and I have studie this and the rest of this topic.
If I understand everything right it works like this :

function one will be executed.
if fn = right??
?? fn[1] will be add[1, one]
(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(...)`.???

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

On Mon, Jun 9, 2014 at 12:29 PM, Roelof W. [email protected] wrote:

Thanks, still I wonder why on the simple case the x is not needed .

What do you mean by ‘the simple case’?

Jesus.

Jes??s Gabriel y Gal??n schreef op 9-6-2014 12:37:

On Mon, Jun 9, 2014 at 12:29 PM, Roelof W. [email protected] wrote:

Thanks, still I wonder why on the simple case the x is not needed .
What do you mean by ‘the simple case’?

Jesus.

this one :

def one fn=nil
if fn
fn[1]
else
1
end
end

->(y) { y + x }
end

Jes??s Gabriel y Gal??n schreef op 9-6-2014 12:37:

On Mon, Jun 9, 2014 at 12:29 PM, Roelof W. [email protected] wrote:

Thanks, still I wonder why on the simple case the x is not needed .
What do you mean by ‘the simple case’?

Jesus.

This one :

On Mon, Jun 9, 2014 at 12:50 PM, Roelof W. [email protected] wrote:

``````         ^^^
``````->(y) { y + x }