Implementing “last” in Prolog

Question 1: Prolog systems are not always able to decide whether or not a clause will apply prior to executing it. The precise circumstances are implementation dependent. That is, you cannot rely on that decision in general. Systems do improve here from release to release. Consider as the simplest case: ?- X = 1 ; … Read more

Pythonic way to convert a list of integers into a string of comma-separated ranges

>>> from itertools import count, groupby >>> L=[1, 2, 3, 4, 6, 7, 8, 9, 12, 13, 19, 20, 22, 23, 40, 44] >>> G=(list(x) for _,x in groupby(L, lambda x,c=count(): next(c)-x)) >>> print “,”.join(“-“.join(map(str,(g[0],g[-1])[:len(g)])) for g in G) 1-4,6-9,12-13,19-20,22-23,40,44 The idea here is to pair each element with count(). Then the difference between the … Read more

How to count items’ occurence in a List

Play around with this: var elements = [“a”, “b”, “c”, “d”, “e”, “a”, “b”, “c”, “f”, “g”, “h”, “h”, “h”, “e”]; var map = Map(); elements.forEach((element) { if(!map.containsKey(element)) { map[element] = 1; } else { map[element] += 1; } }); print(map); What this does is: loops through list elements if your map does not have … Read more