문제

I'm trying to understand lambda calculus with procs and ruby. Here is some code:

puts -> x { -> y {x.call(y) } }
# => #<Proc:0x2a3beb0@C:/first-ruby.rb:1 (lambda)>

puts -> x { x + 2}.call(1)
# => 3

What does -> signify in above example? Is the call method passing the value to the caller, so in the first example, value y is passed to y and in the second example, 1 is passed to x? In the second example, why is 1 evaluated to x?

도움이 되었습니까?

해결책 2

What does -> signify in above example?

-> is part of the literal syntax for lambdas, just like, say, ' is part of the literal syntax for strings.

Is the .call method just passing the value from to caller,

The call method is the method, which, well, calls (or executes) the lambda. The arguments to the call method are bound to the parameters of the lambda.

so in first example value y is passed to y and in second example 1 is passed to x.

No, in the first example, y is passed to the outer lambda and bound to its x parameter. In the second example, 1 is passed to the lambda and bound to its x parameter.

In second example why how is 1 evaluated to x?

1 does not evalute to x. 1 is an immediate value, and in Ruby, immediate values always evaluate to themselves. 1 will always evaluate to 1, never to x or anything else.

다른 팁

This is a shortcut for the pure lambda expression:

lmbd = -> arg{ something to do with arg } # With ->{} notation

lmbd = lambda { |arg| something to do with arg } # Standard notation

In your first example you invoke puts method with Proc(lambda) object, and that's why you see #<Proc:0x2a3beb0@C:/first-ruby.rb:1 (lambda)> in the output.

In the second example you invoke puts with lmbd.call(1) method, i.e. puts outputs the result of lambda calculation.

So, if you have lmbd variable which is lambda object, you can pass it like any argument and then get it's result by invoke lmbd.call():

lmbd = -> greeting{ puts "#{greeting}, lambda-expression!" }

def say_hello l, text
    l.call(text)
end

say_hello lmbd, "Aloha" # => Aloha, lambda-expression!

Let's define a function using Ruby lambda.

def plus_two # no args here
  ->(x) {x + 2} # args go here
end

# assign a value
x = 1

# call it
plus_two.call(x)
# => 3

Your first example is a bit more complex but using this idea you should be able to come up with functional methods. I'm studying Scala and functional programming is based upon these substitution principles.

Try doing some recursion using these. It's like calling functions of functions n times. What would be the base case then?

As for the Lambda Calculus https://github.com/mackorone/lambda/blob/master/intro.pdf

Try to keep things simple and show the steps rather than trying to figure out what a one liner is doing. Yes they are nice but if you can't read it you can't understand it.

Here's something I was just recently working on:

require 'date'

num = DateTime.now.to_time.utc.to_datetime.ajd - 2451545.0
@t = num / 36525.0

# the terms in reverse order form for the array
@l0_a = [1.0/-19880000.0,
         1.0/-152990.0,
         1.0/499310.0,
         0.0003032028,
         36000.76982779,
         280.4664567]

 # make an enumerator
 @l0_e = @l0_a.each

 # make a lambda to pass the enumerator to.
 def my_lambda
   ->(x) {x.reduce {|acc, el| acc * @t + el} % 360}
 end

 puts  my_lambda.call(@l0_e)

This is mean longitude of the sun formula using enumerator methods and of course a lambda.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top