How to extend a Scala list to enable slicing not by explicit position but by given predicate/condition

List already has a slice method – it takes a subset of elements between a start and end index. What you’re looking for is repeated application of the span method: def span(p: (A) ⇒ Boolean): (List[A], List[A]) Which is documented as: Splits this list into a prefix/suffix pair according to a predicate. Note: c span … Read more

Need to partition a list into lists based on breaks in ascending order of elements (Haskell)

You can do this by resorting to manual recursion, but I like to believe Haskell is a more evolved language. Let’s see if we can develop a solution that uses existing recursion strategies. First some preliminaries. {-# LANGUAGE NoMonomorphismRestriction #-} — because who wants to write type signatures, amirite? import Data.List.Split — from package split … Read more

Intersection and union of 2 lists

Also, not sure why you’re dead against cuts, so long as their removal would not change the declaritive meaning of the code, as per your link. For example: inter([], _, []). inter([H1|T1], L2, [H1|Res]) :- member(H1, L2), inter(T1, L2, Res). inter([_|T1], L2, Res) :- inter(T1, L2, Res). test(X):- inter([1,3,5,2,4], [6,1,2], X), !. test(X). X = … Read more

“bad words” filter [closed]

Beware of clbuttic mistakes. “Apple made the clbuttic mistake of forcing out their visionary – I mean, look at what NeXT has been up to!” Hmm. “clbuttic”. Google “clbuttic” – thousands of hits! There’s someone who call his car ‘clbuttic’. There are “Clbuttic Steam Engine” message boards. Webster’s dictionary – no help. Hmm. What can … Read more

How to count number of element occurrences in a list in Prolog

The following is based on my previous answer to “Remove duplicates in list (Prolog)” and on this previous answer to the question “Prolog union for A U B U C“. list_item_subtracted_count0_count/5 is derived from list_item_subtracted/3. list_counts/2 is derived from list_setB/2, which were both defined here. list_item_subtracted_count0_count([], _, [], N,N). list_item_subtracted_count0_count([A|As], E, Bs1, N0,N) :- if_(A … Read more