Get elements from list of lists

You asked for all elements of a list of lists. That is, for [[1,2,3],[4]] this would be the list [1,2,3,4]. However, for [[[1],[3]]] this would be the list [[1],[3]] since [1] and [3] are elements. For this reason, flatten/2 is incorrect it gives you [1,3] as an answer. Also, for 1 it gives [1]… Here … Read more

Decrementing ranges in Haskell

Basically, because [10..1] is translated to enumFromTo 10 1 which itself has the semantics to create a list by taking all elements less-than 1 which result from counting upward (with step-size +1) from (including) 10. Whereas [10, 9..1] is translated to enumFromToThen 10 9 1 which explicitly states the counting step-size as 9-10, i.e. -1 … Read more

Subsets in Prolog

Here goes an implementation: subset([], []). subset([E|Tail], [E|NTail]):- subset(Tail, NTail). subset([_|Tail], NTail):- subset(Tail, NTail). It will generate all the subsets, though not in the order shown on your example. As per commenter request here goes an explanation: The first clause is the base case. It states that the empty list is a subset of the … Read more

Remove duplicates in list (Prolog)

You are getting multiple solutions due to Prolog’s backtracking. Technically, each solution provided is correct, which is why it is being generated. If you want just one solution to be generated, you are going to have to stop backtracking at some point. This is what the Prolog cut is used for. You might find that … Read more

Search for an item in a Lua list

You could use something like a set from Programming in Lua: function Set (list) local set = {} for _, l in ipairs(list) do set[l] = true end return set end Then you could put your list in the Set and test for membership: local items = Set { “apple”, “orange”, “pear”, “banana” } if … Read more

Hide Show content-list with only CSS, no javascript used

I wouldn’t use checkboxes, i’d use the code you already have DEMO http://jsfiddle.net/6W7XD/1/ CSS body { display: block; } .span3:focus ~ .alert { display: none; } .span2:focus ~ .alert { display: block; } .alert{display:none;} HTML <span class=”span3″>Hide Me</span> <span class=”span2″>Show Me</span> <p class=”alert” >Some alarming information here</p> This way the text is only hidden on … Read more

Scala: Remove duplicates in list of objects

list.groupBy(_.property).map(_._2.head) Explanation: The groupBy method accepts a function that converts an element to a key for grouping. _.property is just shorthand for elem: Object => elem.property (the compiler generates a unique name, something like x$1). So now we have a map Map[Property, List[Object]]. A Map[K,V] extends Traversable[(K,V)]. So it can be traversed like a list, … Read more