# Can some help with Scala code for below Currying and partial functions in scala

Write a function factorial which gives the factorial of the given number

Write a partially applied function customAdd from add such that the output is factorial(input1) + factorial(input2)

import java.io._
import java.math._
import java.security._
import java.text._
import java.util._
import java.util.concurrent._
import java.util.function._
import java.util.regex._
import java.util.stream._
import scala.collection.immutable._
import scala.collection.mutable._
import scala.collection.concurrent._
import scala.concurrent._
import scala.io._
import scala.math._
import scala.sys._
import scala.util.matching._
import scala.reflect._

object Result {

``````/*
* Complete the 'CurryFunction' function below.
*
* The function accepts following parameters:
*  1. INTEGER input1
*  2. INTEGER input2
*/

def CurryFunction(input1: Int, input2: Int) {
def factorial //Define the method factorial
// Define the add function here.
val customAdd = add(input1)
println(customAdd(input2))
}
``````

}

object Solution {
def main(args: Array[String]) {
val input1 = StdIn.readLine.trim.toInt

``````    val input2 = StdIn.readLine.trim.toInt

Result.CurryFunction(input1, input2)
}
``````

}

Can you show us the code youâ€™ve written so far and explain where youâ€™re stuck in making more progress on it? Then we can help you get unstuck.

2 Likes

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;

``````      //val customAdd = add(input1)_
// println(customAdd(input2))
}``````

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))
}``````
1 Like

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
}
}
``````
1 Like

Thanksâ€¦!