valid bracket list in prolog
问题 I'm trying to test if a list of brackets is valid. My code: checkbrackets([]). checkbrackets(['('|T]):- T = [')'|List], checkbrackets(List). checkbrackets(['('|T]):- T = ['('|List], append(Rest,[')'],T), checkbrackets(Rest). My code works for ['(', '(', ')', '(', '(', ')', ')', ')'] but it fails for ['(', '(', ')', ')', '(', ')'] . What am I doing wrong? Is it possible to write such a test without additional arguments like counters? 回答1: For the sake of completeness, here is a solution