https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

alex cole

11/25/2020, 12:50 AM
Hi, I am wanting to replicate a python feature
Copy code
def myFunction(a,b,c): 
    return lambda x: a * x ** 2 + b * x + c
however I am not understanding what is wrong with
Copy code
fun potentialEnergy(charge1, charge2, k){
    val x = {position: Double -> k * charge1 * charge2 / position / position} // creating lambda function
    return x
}
What is wrong with this lambda function expression.
1
I am doing this in jupyter
Example7.2
m

Martin Nowosad

11/25/2020, 5:53 AM
you need to specify types for your function parameters. That code should not compile at all
m

Milan Hruban

11/25/2020, 7:38 AM
an equivalent of the python function above would be (assuming all parameters are `Double`s:
Copy code
import kotlin.math.pow

fun myFunction(a: Double, b: Double, c: Double) = { x: Double -> a * x.pow(2.0) + b * x + c }
👍 1
a

alex cole

11/25/2020, 4:09 PM
can you explain the "=" notation?
n

nanodeath

11/25/2020, 4:36 PM
https://kotlinlang.org/docs/reference/basic-syntax.html#defining-functions expression syntax. it looks a little weird because this function is itself returning a function (well, a lambda)
2 Views