Prolog, find minimum in a list

It is common to use a so-called “lagged argument” to benefit from first-argument indexing: list_min([L|Ls], Min) :- list_min(Ls, L, Min). list_min([], Min, Min). list_min([L|Ls], Min0, Min) :- Min1 is min(L, Min0), list_min(Ls, Min1, Min). This pattern is called a fold (from the left), and foldl/4, which is available in recent SWI versions, lets you write … Read more

My async call is returning before list is populated in forEach loop

This code Future<List<String>> readHeaderData() async { List<String> l = new List(); List<String> files = await readHeaders(); // Gets filenames files.forEach((filename) async { final file = await File(filename); String contents = await file.readAsString(); User user = User.fromJson(json.decode(contents)); String name = user.NameLast + “, ” + user.NameFirst; print(name); l.add(name); } return l; } returns the list l … Read more

Recursive Prolog predicate for reverse / palindrome

Ad 1: It is impossible to define reverse/2 as a (directly edit thx to @repeat: tail) recursive predicate – unless you permit an auxiliary predicate. Ad 2: palindrome(X) :- reverse(X,X). But the easiest way is to define such predicates with DCGs: iseq([]) –> []. iseq([E|Es]) –> iseq(Es), [E]. reverse(Xs, Ys) :- phrase(iseq(Xs), Ys). palindrome(Xs) :- … Read more

Is there a safe way in Scala to transpose a List of unequal-length Lists?

How about this: scala> def transpose[A](xs: List[List[A]]): List[List[A]] = xs.filter(_.nonEmpty) match { | case Nil => Nil | case ys: List[List[A]] => ys.map{ _.head }::transpose(ys.map{ _.tail }) | } warning: there were unchecked warnings; re-run with -unchecked for details transpose: [A](xs: List[List[A]])List[List[A]] scala> val ls = List(List(1, 2, 3), List(4, 5), List(6, 7, 8)) ls: … Read more