Skip to content

Instantly share code, notes, and snippets.

@hiroshi-maybe
Created June 14, 2019 20:57
Show Gist options
  • Save hiroshi-maybe/2ac4a487faff8098ef19843089b60ea9 to your computer and use it in GitHub Desktop.
Save hiroshi-maybe/2ac4a487faff8098ef19843089b60ea9 to your computer and use it in GitHub Desktop.
import Foundation
indirect enum Exp {
case add(Exp, Exp)
case sub(Exp, Exp)
case num(Int)
func apply() -> Int {
switch self {
case let .add(e1, e2):
return e1.apply() + e2.apply()
case let .sub(e1, e2):
return e1.apply() - e2.apply()
case let .num(n):
return n
}
}
}
let e1 = Exp.add(
.num(1),
.sub(
.num(2),
.add(
.add(
.add(
.num(2),
.sub(
.add(
.num(3),
.num(4)),
.num(1)
)
),
.add(
.num(2),
.num(100)
)
),
.sub(
.num(100),
.num(101)
)
)
)
)
print(e1.apply())
/*
Your previous C++ content is preserved below:
// This is a sandbox to experiment with CoderPad's execution capabilities.
// It's a temporary, throw-away session only visible to you.
#include <iostream>
using namespace std;
// To execute C++, please define "int main()"
int main() {
auto words = { "Hello, ", "World!", "\n" };
for (const string& word : words) {
cout << word;
}
return 0;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment