I'm reading Jorge Gajon's Trees as Linked Lists in Common Lisp, which includes a description of a tree done in Lisp. The author gives this basic example:
Then gives a Lisp list representaton:
(1 (2 6 7 8) 3 (4 (9 12)) (5 10 11))
where position implies hierarchy level: 1 is first in the list, i.e., it's at the top, 2 is at the next level, but it's the top of its level, etc. But then he gives this caveat:
PLEASE NOTE, that if you need to represent trees in a production program you shouldn’t use lists as described here unless you have a good reason. This is only an exercise in understanding how cons cells work.
All right, how should one represent the tree data structure in "production" code? BTW, I'd also like to see an example of an acyclic directed graph, i.e., something tree-like that also has "multiple parent" capabilities. For example in the diagram above, 8 is a child of 2, but also 3. I'm guessing something like this:
(1 (2 6 7 8) (3 8) (4 (9 12)) (5 10 11))
but it seems like I've created a "shadow" twin of 8 and not really relating how the 8 that is the child of 3 is the same 8 that also has 2 as a parent. This problem gets worse if I wanted to have 3 as 12's parent.
(1 (2 6 7 8) (3 8 12) (4 (9 12)) (5 10 11))
The fact that 12 is in a lower hierarchy gets lost in the shuffle, so to speak.
Is there a good and proper treatment (book, etc.) of data structures in the Lisp/Scheme/Clojure world? I've only found one-off stuff like this.
Assuming you have quicklisp installed, and you should, then consider the libaries enumerated by: (ql:system-apropos "graph"), and also try cliki http://cliki.net/site/search?query=graph
The 'production' code would use CLOS (the Common Lisp Object System) to define a NODE class and operations over the graph/tree.
Related
I'm trying to join the lists '(1 2 3) and '(4 5 6) so that they become '(1 2 3 4 5 6). In Clojure for example, this function is called concat.
I tried searching for the answer on the internet but could only find people trying to implement more advanced merge strategies for the lists.
In scheme this function is called append.
I'm reviewing for my midterm and this specific question is causing me some issues.
This is the following array to perform the binary search:
the value I want to search for is 150.
To start off, I take the first element which is 0, and the last element which is 15.
(start + end) / 2,
(0 + 15) / 2 = 7
The value at the array of 7 is 90.
90 < 150, so the value is contained in the right side of the array.
The array now looks like this:
Continuing with the same logic
(start + end) / 2
(8 + 15) / 2 = 11.
However, according to the professor I should be at the value 12 here. I'm not sure what i am doing wrong. Any help would be appreciated.
The algorithms were written even before the computers were invented.
Computers are simply a tool or a device which implements the algorithm in an efficient manner which is why it is fast.
The binary search which you are performing here is relevant to computers as the array are indexed from 0 (counting usually starts from 0 in computers), that is why you are getting 11 which is correct in point of computers.
But for the humans counting starts from 1 and the so the result according to professor is 12.
While writing algorithms we write in according to the perception of the human and we twist it a little to implement in our machine.
This question already has answers here:
Writing an algorithm to decide whether a target number can be reached with a set of other numbers and specific operators?
(3 answers)
Closed 8 years ago.
Here's the problem:
Given 4 numbers, I need to give a calculated process which results 24. All the operations I can use are addition, subtraction, multiplication, division. How to print the calculated process?
Ex:
Input: 4,7,8,8
Output: (7-(8/8))*4=24.
(The following is an expansion on an idea suggested by Sayakiss)
One option would be enumerating all possible combinations of numbers and arithmetic operations performed on them.
If you have 4 numbers, there are only 24 different ways to write them in a list (the following example is for numbers 4, 7, 8, 9 - i changed the last number in your example to make them all different):
4 7 8 9
4 7 9 8
4 8 7 9
4 8 9 7
...
9 8 7 4
If some numbers are identical, some of the above lists will appear twice (not a problem).
For each of the above orderings, there are 64 different ways to insert an arithmetic operation between the numbers:
4+7+8+9
4+7+8-9
4+7+8*9
4+7+8/9
4+7-8+9
...
4/7/8/9
For each of the above sequences, there are 5 ways to place parentheses:
((4-7)-8)-9
(4-7)-(8-9)
(4-(7-8))-9
4-((7-8)-9)
4-(7-(8-9))
When you combine all 3 "aspects" mentioned above, you get 24 * 64 * 5 = 7680 expressions; evaluate each one and check whether its value is 24 (or whatever number you need it to be).
It may be convenient to generate the expressions in a tree form, to simplify evaluation (this depends on the programming language you want to use; e.g. in C/C++ there is no eval function) . For example, the expression 4*((7-8)+9) may be represented by the following tree:
*
/ \
4 +
/ \
- 9
/ \
7 8
Some notes:
You may want to tweak the choice of arithmetic operations to allow for expressions like 47+88 - not sure whether the rules of your game permit that.
Many of the evaluated expressions may be annoyingly verbose, like ((4+7)+8)+8 and 4+(7+(8+8)) (which are also examined twice, with the order of the 8's switched); you could prevent that by inserting some dedicated checks into your algorithm.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
What does # mean in LISP
I am learning lisp, but one thing I do not understand is why it is necessary for #' to be used. If a function with a specific name exists, why would lisp think its a variable?
Ex:
>(remove-if-not #'evenp '(1 2 3 4 5 6 7 8 9 10))
(2 4 6 8 10)
Some lisps, like Common Lisp, require this. Others, like Scheme, do not.
You need to do this because the Lisp you're using has a separate namespace for functions versus "normal" variables. If you left out the #' then the symbol evenp would be interpreted as referring to the "normal" (non-function) namespace.
The read syntax
#'X
means exactly the same thing as
(FUNCTION X)
(FUNCTION X) means, roughly, "resolve the symbol X in the namespace of functions". Without this, X is evaluated as a variable. Functions and variables are in separate namespaces in Common Lisp. This is a rule.
As to the question, why would Lisp think it is a variable? Let's put it another way: given that there are two namespaces, why can't Lisp just fall back automatically on one or the other if there is no ambiguity? The reason is that that would be a hack compared to just Lisp-1 or Lisp-2, worse than either of them. Lisp-1 and Lisp-2 are words used in the Lisp culture to refer to dialects that have one a single namespace for variables and those that have two.
If you want to know more about the pros and cons of doing it one way or another, it's all in this paper by Kent Pitman and Richard Gabriel: http://www.nhplace.com/kent/Papers/Technical-Issues.html [Technical Issues of Separation in Function Cells and Value Cell].
As an aside: you can use 'FUNC or (QUOTE FUNC) instead: (remove-if-not 'evenp ...). This is a very late binding mechanism that goes through the symbol, and will not work for lexical functions. It probably won't compile very efficiently. Calling through 'FUNC always has to do a lookup through the symbol, whereas #'FUNC (at least in some important situations) can be optimized.
I want to solve Project Euler's problem #68 in C#, but I've so far not understood the question clearly. What does external node mean in this problem statement?
Consider the following "magic" 3-gon ring, filled with the numbers 1
to 6, and each line adding to nine.
4
\
3
/ \
1 - 2 - 6
/
5
Working clockwise, and starting from the group of three with the
numerically lowest external node (4,3,2 in this example), each
solution can be described uniquely. For example, the above solution
can be described by the set: 4,3,2; 6,2,1; 5,1,3.
'External node' is a node not included in the inner triangle (pentagon). On the first picture, 4, 5 and 6 are external nodes.
Regarding 'helping to understand the question', what other parts confuse you?
edit
In the first sentence it says 'each line adding to nine', 9 here is the total. You can calculate the 'total' of each solution by summing up numbers in any of 3 lines.
#Kristo Aun: Think of the '4,3,2; 6,2,1; 5,1,3' > '4,2,3; 5,3,1; 6,1,2' as numbers, which means 432621513 > 423531612. The numbers come from a single line in any order, though you need to start clockwise.
In the task, they say "By concatenating each group it is possible to form 9-digit strings; the maximum string for a 3-gon ring is 432621513."
What is meant by 'maximum'? How come '4,3,2; 6,2,1; 5,1,3' > '4,2,3; 5,3,1; 6,1,2'? It certainly doesn't make sense in terms of set theory...