Having trouble representing and understanding sets - lisp

Specifically, the concept of set referred to here: http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-16.html#%_sec_2.3.3
I understand how the data structures work and how to traverse through them, but the use of it is tougher to conceptualize.
Would someone mind explaining it in different words, perhaps that might help it click. Thanks so much.

If you have a set (a b c), then trying to insert a into it will result in the same set (a b c). It is just a collection that has the constraint/guarantee that no value will be duplicate.
Example use: You want to find all words used in a text, but their frequencies are irrelevant. If you have a set, then the algorithm is just: go through all words and add each into the set. Since the set automatically throws away all duplicates, it is the correct result when you finish.

Related

Converting binary to decimal within a case structure in LabVIEW 2018

I have two CONTROLs (buttons specifically) that when activated act as one bit each.
So it basically means the highest number I can produce is 2 by having both buttons activated at the same time. EDIT: Okay what I meant to say was that the highest output I'm going to be able to produce is two because I only have 2 buttons, each representing a 1. So 1+1=2.
However, this is only understood logically because the bits are yet to be converted to a numerical(decimal) format. I can use a 'Boolean to 0,1' converted directly to get the values but I'm instructed to use a case structure to complete this.
Right now I'm completely perplexed because a case structure needs exactly ONE case selector but I have TWO buttons. Secondly, this problem seems way too SIMPLE to require a case structure therefore making it genuinely harder to use a more complex method.
So it basically means the highest number I can produce is 2 by having both buttons activated at the same time.
A 2bit-number can have four values, 0...3, hmm?
In general, if the two booleans are bits of the number, or the number can somehow be calculated from the bools, do it.
But if the number can have predefined values which depend on the booleans, but can not be calculated from them, you need some other kind of case distinction. Maybe, whoever instructed you, had this in mind.
You can make a case structure for the first boolean, and in each case, insert a second case structure for the second boolean. This is good when there will be more complex code and logic depending on the boolenans, so you can easily concentrate on one combination of values. For simple cases, this lacks overview, and when adding a third boolean, it's lots of work.
Calculate an interim value, and connect it to a single case structure. Now, there is only one case structure, but you have no overview over all cases. Note I've changed the radix of the case struture to boolean, so you can see the bits in the selector.
Use a simple array to take a value from
Create a lookup table with predefined conditions and values
(Note that the first two solutions force you to implement each case, while the last two do not - what if your arrays are of size 3, only?)

Database and item orders (general)

I'm right now experimenting with a nodejs based experimental app, where I will be putting in a list of books and it will be posted on a forum automatically every x minutes.
Now my question is about order of these things posted.
I use mongodb (not sure if this changes the question or not) and I just add a new entry for every item to be posted. Normally, things are posted in the exact order I add them.
However, for the web interface of this experimental thing, I made a re-ordering interaction where I can simply drag and drop elements to reorder them.
My question is: how can I reflect this change to the database?
Or more in general terms, how can I order stuff in general, in databases?
For instance if I drag the 1000th item to 1st order, everything below needs to be edited (in db) between 1 and 1000 the entries. This does not seem like a valid and proper solution to me.
Any enlightenment is appreciated.
An elegant way might be lexicographic sorting. Introduce a String attribute for each item. Make the initial length of the values large enough to accomodate the estimated number of items. E.g., if you expect 1000 items, let the keys be baa, bab, bac, ... bba, bbb, bbc, ...
Then, when an item is moved from where it is to another place between two items, assign a value to the sorting attribute of the moved item that is somewhere equidistant (lexicographically) to those items. So to move an item between dei and dej, give it the value deim. To move an item between fadd and fado, give it the value fadi.
Keys starting with a were initially not used to leave space for elements that get dragged before the first one. Never use the key a, as it will be impossible to move an element before this one.
Of course, the characters used may vary according to the sort order provided by the database.
This solution should work fine as long as elements are not reordered extremely frequently. In a worst case scenario, this may lead to longer and longer attribute values. But if the movements are somewhat equally distributed, the length of values should stay reasonable.

Is it possible to assign a number to a lists with the same name to be differentiated?

A part of my program relies on recording the lengths of roads within my user interface as they are drawn out. As this requires looping , and as I want to be able to keep the name of the list all the data is stored in the same, is it possible to create lists thusly :
set list road-length X
(where X is a counter that is incremented every time a condition is met). Essentially can I tag numbers on to the ends of lists so that I can tell them apart when they need to be read later on in my program?
You can, but it is almost surely the wrong approach. (For one thing, the names will not be global unless you declare them all ahead of time.) Instead, use the table extension, create a global to hold your table, and use the table to map your id numbers to your lists. This will prove much more useful.

Why does Map addition of duplicates only take the last key's value

Per this question:
Best way to merge two maps and sum the values of same key?
I would need to use scalaz to get what I want, however I am curious if anybody knew why the below does not work as I expect?
Map(1->2.0)+(1->1.0) //Map(1->1.0)
I would expect this to result in Map(1->3.0). But, it appears that maps only return the last key as shown by:
Map(1->1.0, 1->3.0) //Map(1->3.0)
So, based on the documentation
Adds two or more elements to this collection and returns a new collection.
and the above, my guess is that the map might store the values, but only return the last item? This is just not my intuition of what the add should do...maybe it is an efficiency move.
Once I have more of a moment, I will take a look at the code and try to figure it out from there, but wanted to ask here in case somebody already knew?
It has nothing to do with efficiency; it's typing. Map plus elements returns a map of compatible type. You don't know the type, so you can't know to add numbers. You could list them instead, but Seq(2.0,1.0) is not a supertype of 2.0. So you'd end up having a map to Any, which doesn't help you out at all when it comes to keeping your types straight, and you wouldn't have any way to replace an existing element with another.
So, + adds a new element if the key does not exist, or replaces the existing element if the key does exist. (The docs should say so, though.)
If you want the "other" behavior, you need a more complex transformation, and that's what Scalaz' |+| will do for you for the natural addition on the elements.
#RexKerr's answer is perfectly correct, but I think doesn't emphasise the crucial misunderstanding here.
The + operation on a Map means put - it puts a new key/value pair into the map (potentially replacing the existing pair for that key). It doesn't have anything to do with addition (and Rex's answer explains further how it cannot necessarily have anything to do with addition). You seem to come from a C# background, so you should think of:
myMap + (1, 1.0)
as being
myMap[1] = 1.0
The ability to insert a new key/value pair is a fundamental operation of a Map/Dictionary datatype. The ability you want to encode is something much less fundamental (and a special case for a more general ability to merge maps by key, as is mentioned in the question you reference, and here).

Why no immutable double linked list in Scala collections?

Looking at this question, where the questioner is interested in the first and last instances of some element in a List, it seems a more efficient solution would be to use a DoubleLinkedList that could search backwards from the end of the list. However there is only one implementation in the collections API and it's mutable.
Why is there no immutable version?
Because you would have to copy the whole list each time you want to make a change. With a normal linked list, you can at least prepend to the list without having to copy everything. And if you do want to copy everything on every change, you don't need a linked list for that. You can just use an immutable array.
There are many impediments to such a structure, but one is very pressing: a doubly linked list cannot be persistent.
The logic behind this is pretty simple: from any node on the list, you can reach any other node. So, if I added an element X to this list DL, and tried to use a part of DL, I'd face this contradiction: from the node pointing to X one can reach every element in part(DL), but, by the properties of the doubly linked list, that means from any element of part(DL) I can reach the node pointing to X. Since part(DL) is supposed to be immutable and part of DL, and since DL did not include the node pointing to X, that just cannot be.
Non-persistent immutable data structures might have some uses, but they are generally bad for most operations, since they need to be recreated whenever a derivative is produced.
Now, there's the minor matter of creating mutually referencing strict objects, but this is surmountable. One can use by-name parameters and lazy vals, or one can do like Scala's List: actually create a mutable collection, and then "freeze" it in immutable state (see ListBuffer and it's toList method).
Because it is logically impossible to create a mutually (circular) referential data-structure with strict immutability.
You cannot create two nodes that point to each other due to simple existential ordering priority, in that at least one of the nodes will not exist when the other is created.
It is possible to get this circularity with tricks involving laziness (which is implemented with mutation), but the real question then becomes why you would want this thing in the first place?
As others have noted, there is no persistent implementation of a double-linked list. You will need some kind of tree to get close to the characteristics you want.
In particular, you may want to look at finger trees, which provide O(1) access to the front and back, amortized O(1) insertion to the front and back, and O(log n) insertion elsewhere. (That's in contrast to most other commonly-used trees which have O(log n) access and insertion everywhere.)
See also:
video explanation of finger trees (by the implementor of finger trees in clojure.contrib)
finger tree implementation in Scala (I haven't used it personally, but it's the top google hit)
As a supplemental to the answer of #KimStebel I like to add:
If you are searching for a data structure suitable for the question that motivated you to ask this question, then you might have a look at Extreme Cleverness: Functional Data Structures in Scala by #DanielSpiewak.