## 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

## Grouping a list into lists of n elements in Haskell

A quick search on Hoogle showed that there is no such function. On the other hand, it was replied that there is one in the split package, called chunksOf. However, you can do it on your own group :: Int -> [a] -> [[a]] group _ [] = [] group n l | n > … Read more

## Slicing a list into a list of sub-lists [duplicate]

[input[i:i+n] for i in range(0, len(input), n)] # Use xrange in py2k where n is the length of a chunk. Since you don’t define what might happen to the final element of the new list when the number of elements in input is not divisible by n, I assumed that it’s of no importance: with … 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

## What are the differences between vector and list data types in R?

You can’t have an unsequence :: (Monad m) => m [a] -> [m a]. The problem lies with lists: you can’t be sure how may elements you are going to get with a list, and that complicates any reasonable definition of unsequence. Interestingly, if you were absolutely, 100% sure that the list inside the monad … 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

## Cons element to list vs cons list to element in Scheme

The primitive cons simply sticks together two things, the fact that some of those things are considered lists is incidental. For instance, this works and creates a pair (also known as a cons cell): (cons 1 2) => ‘(1 . 2) ; a pair Now, if the second argument to cons happens to be a … Read more

## Counting unique elements in a list

Using Data.Map and tuple sections: count = Map.fromListWith (+) . map (, 1) (Add Map.toList if you need a list.)