Skip to content

Instantly share code, notes, and snippets.

@esehara
Last active August 21, 2022 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esehara/6486846 to your computer and use it in GitHub Desktop.
Save esehara/6486846 to your computer and use it in GitHub Desktop.
Prolog de Kakeibo
% it is fiction!!
accounts(給料, 200000).
accounts(家賃, 60000).
accounts(借金, 10000).
% vim: set syntax=prolog:
income(給料).
expense(家賃).
expense(借金).
#!swipl -q -g main -s
:- use_module(library(apply)).
:- use_module(library(lists)).
:- include(category).
:- include(accounts).
:- include(tools).
main :-
print('
==================================
Logical Kakeibo
==================================
\n'),
account_list(INCOMES, EXPENSES),
costs(INCOMES, INCOME),
costs(EXPENSES, EXPENSE),
format("収入 ~f円\n", INCOME),
format("出費 ~f円\n", EXPENSE),
format("残り ~f円\n", INCOME - EXPENSE),
halt.
% vim: set syntax=prolog:
isincome((KIND, _)) :- income(KIND).
getcost((_, COST), COST).
costs(ACCOUNTLIST, COST) :-
maplist(getcost, ACCOUNTLIST, COSTS),
sumlist(COSTS, COST).
account_list(INCOMES, EXPENSES) :-
findall((X, Y), accounts(X, Y), ACCOUNTS),
partition(isincome, ACCOUNTS, INCOMES, EXPENSES).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment