With out using Partial Curry Function. I did in straight forward way.
def CurryFunction(input1: Int, input2: Int) {
var f: Int = 1
var f1: Int = 1
def factorial(input1: Int) {
for(i: Int<-1 to input1)
{
f = f * i;
}
// println(f)
} //Define the method factorial
def factorial1(input2: Int) {
for(j: Int<-1 to input2)
{
f1 = f1 * j;
}
//println(f1)
}
factorial(input1)
factorial1(input2)
var fact = f + f1
println(fact)
// Define the add function here.
//def add2(a: Int) (b: Int) = a + b;
Can you edit your post and use Markdown so the code is properly indented? In unindented form, it’s really hard to read, which makes it hard to help you.
def CurryFunction(input1: Int, input2: Int) {
var f: Int = 1
var f1: Int = 1
def factorial(input1: Int) {
for(i: Int<-1 to input1) {
f = f * i;
}
} //Define the method factorial
def factorial1(input2: Int) {
for(j: Int<-1 to input2) {
f1 = f1 * j;
}
}
factorial(input1)
factorial1(input2)
var fact = f + f1
println(fact)
// Define the add function here.
//val customAdd = add(input1)_
// println(customAdd(input2))
}
A curry function is just a function with multiple parameter lists, as such as that calling the function with just one parameter lists returns another function.
So you only need to do this: def curryFunction(input1: Int)(input2: Int): Int = { and you would be done.
However, your implementation has a couple of problems.
First, you define factorial twice which is probably something your professor would penalize.
Second, those factorials are mutable where they could be easily defined using recursion, but that is not that bad. The really bad thing is that they are mutating a value outside of their scope (the good thing is that those are inside the scope of the bigger function).
So I would guess that at the end you should be having something like this:
object Result {
def factorial(input: Int): Int = {
// Implementation leave out as an exercise for the reader.
}
def customAdd(input1: Int)(input2: Int): Int = {
val fact1 = factorial(input1)
val fact1 = factorial(input2)
fact1 + fact2
}
}