0
votes

I'm trying to return a tuple of a list and map. I've already gotten the list to compile correctly however I'm not sure how to use a map and list to get a map of the keys and values that match what's in my list. Here is what I have so far:

I've been able to achieve returning a list. However I need to return (List[Double], Map[String, Double])

    def emulateSingleInstruction(stack: List[Double],
                             env: Map[String, Double],
                             ins: StackMachineInstruction): (List[Double], Map[String, Double]) = {
    ins match{
        case AddI => stack match{
            case i1 :: i2 :: tail => (i1 + i2 :: tail, env)
            case _ => throw new IllegalArgumentException()
        }
        //USE V1 and V2
        case SubI => stack match{
            case i1 :: i2 :: tail => (i1 - i2 :: tail, env)
            case _ => throw new IllegalArgumentException()

        }
        case MultI => stack match{
            case i1 :: i2 :: tail => (i1 * i2 :: tail, env)
            case _ => throw new IllegalArgumentException()
        }
        //USE V1 and V2
        case DivI => stack match{
            case i1 :: i2 :: tail => (i1 / i2 :: tail, env)
            case _ => throw new IllegalArgumentException()
        }
        case ExpI => stack match{
            case Nil => throw new IllegalArgumentException()
            case head :: tail => {
                (scala.math.exp(head) :: tail,env)
            }
        }
        case LogI => stack match{
            case Nil => throw new IllegalArgumentException()
            case head :: tail => {
                if (head > 0){(scala.math.log(head) :: tail,env)}
                else{ throw new IllegalArgumentException()}
            }
        }
        case SinI => stack match{
            case Nil => throw new IllegalArgumentException()
            case head :: tail => {
                (scala.math.sin(head) :: tail,env)
            }
        }
        case CosI => stack match{
            case Nil => throw new IllegalArgumentException()
            case head :: tail => {
                (scala.math.cos(head) :: tail,env)
            }
        }
        case PushI(f) => (f :: stack,env)

        case PopI => stack match{
            case Nil => throw new IllegalArgumentException()
            case i1 :: tail => {
                (tail,env)
            }
        }
    }
}
1

1 Answers

0
votes

Since your example operation seems not to modify the environment but only the stack, I understand you are simply asking how to combine the new stack and the environment in the return value.

def emulateSingleInstruction(stack: List[Double],
                             env: Map[String, Double],
                             ins: StackMachineInstruction): (List[Double], Map[String, Double]) = {
    val newStack = ins match {
        case AddI => // your code. Looks good...
    }
    val newEnv = // expression that evaluates to the updated environment
    (newStack, newEnv)
}