i following along stanford swift course on itunes. in lesson 3 professor writes recursive function evaluate stack of operators , operands.
here code professor has typed it:
class calculatorbrain { private enum op { case operand(double) case unaryoperation(string, double -> double) case binaryoperation(string, (double, double) -> double) } private var opstack = [op]() private var knownops = [string:op]() init() { knownops["×"] = op.binaryoperation("×", *) knownops["+"] = op.binaryoperation("+", +) knownops["-"] = op.binaryoperation("-") { $1 - $0 } knownops["÷"] = op.binaryoperation("÷") { $1 / $0 } knownops["√"] = op.unaryoperation("√", sqrt) } func pushoperand(operand: double) { opstack.append(op.operand(operand)) } func performoperation(symbol: string) { if let operation = knownops[symbol] { opstack.append(operation) } } private func evaluate(ops: [op]) -> (result: double?, remainingops: [op]) { var remainingops = ops if !remainingops.isempty { let op = remainingops.removelast() switch op { case .operand(let operand): return (operand, remainingops) case .unaryoperation(_, let operation): let operandevaluation = evaluate(remainingops) if let operand = operandevaluation.result { return (operation(operand), operandevaluation.remainingops) } case .binaryoperation(_, let operation): // code snipped } } return (nil, ops) } } i compiler error doesn't.
the error on line let operandevaluation = evaluate(remainingops). error "use of local variable 'evaluate' before declaration"
does know why i'm getting error? it's supposed recursive call function evaluate, instead compiler thinks evaluate local variable.
i using xcode 6.4 on os x 10.10.4
thanks!
really strange error. got error, too.
however fix adding bracket if clause several lines below error.
thanks!
Comments
Post a Comment