Skip to content

Instantly share code, notes, and snippets.

@bruteforceboy
Created December 7, 2023 06:24
Show Gist options
  • Save bruteforceboy/35eed82711ecb0d9519e6e72e3905332 to your computer and use it in GitHub Desktop.
Save bruteforceboy/35eed82711ecb0d9519e6e72e3905332 to your computer and use it in GitHub Desktop.
Lambda Equation Eval
#include <bits/stdc++.h>
using namespace std;
/*
@author: wilson
*/
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s;
getline(cin, s);
int n = (int) s.length();
map<string, int> mp;
vector<string> st;
set<string> free_vars;
for (int i = 0; i < n; i++) {
if (s[i] == ')') {
while (st.back() != "(") {
string cur = st.back();
--mp[cur];
if (mp[cur] == 0)
mp.erase(cur);
st.pop_back();
}
string cur = st.back();
--mp[cur];
if (mp[cur] == 0)
mp.erase(cur);
st.pop_back();
continue;
}
if (s[i] == ' ' || s[i] == '.') continue;
if (i + 2 < n && s.substr(i, 2) == "λ") {
++mp[s.substr(i, 3)];
st.emplace_back(s.substr(i, 3));
i += 2;
} else {
string cur = s.substr(i, 1);
st.emplace_back(cur);
++mp[cur];
if (cur != "(" && !cur.empty()) {
string tmp = cur;
cur = "λ" + cur;
if (!mp.count(cur)) {
free_vars.emplace(tmp);
}
}
}
}
if (free_vars.empty()) {
cout << "closed term" << endl;
} else {
vector<string> free_vals(begin(free_vars), end(free_vars));
int m = int(free_vals.size());
for (int i = 0; i < m; i++) {
cout << free_vals[i];
if (i != m - 1)
cout << ", ";
}
cout << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment