Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
This is my first day with Haskell, Can you please explain, how this works.
I assume, first the compiler should work on finding the max value among 3 and 5 and then multiply the result by 2.
Whereas, Haskell multiplies 5*2 and compare the result with 3 and finding the max.
Syntax
max (3 5) * 2
This doesn't mean what you seem to think it means. In the above, the function 3 is applied to the argument 5. Consider instead:
(max 3 5) * 2
Or equivalently:
max 3 5 * 2
Terminology
Let's also keep the terminology straight: the compiler doesn't perform any evaluation, it just produces binaries.
Answer
The first thing to consider is the function *. There is no guaranteed order of evaluation here. To evaluate max 3 5 the max function is applied to each argument and the results is 5. The second argument, 2 is already in normal form. So now we just have 5*2 which produces 10.
You have your parentheses inexplicably backwards.
Haskell multiplies 5*2 and compare the result with 3 and finding the max.
so you want
max 3 (5 * 2)
Your code as written tries to coerce the literal 3 into a function Num a => a -> b, then apply 5 to it. It can't do that, of course, so it stops.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
In the case of a it works. However in case of `b it doesn't. Why does this happen?
a = [0,1]
b = [0,1]
any(map(lambda _:a.append(sum(a[-2:])), range(2,8)))
map(lambda _:b.append(sum(b[-2:])), range(2,8))
print(a)
print(b)
This is extremely obfuscated code. Nobody would write code like this without an ulterior motive (such as Code Golf, or trying to wedge everything onto a single line), as it's doing a whole bunch of strange things that can much more simply be done some other way.
To address the thing you're asking about, any is called to consume all the values being produced by map. The return value of the map call is an iterator that lazily invokes the function it was given (a lambda expression) on successive values of the iterator it was given (a range). However, this code doesn't actually care about the values, which are all None. It wants the lambda function to be run for its side effects, which are appending values to the a or b lists. All of the values are None, since that's what list.append returns. Because None is falsey, the any function consumes all of them without stopping early (as it would do for any truthy value).
A much more natural way to write this code would be:
a = [0, 1]
for _ in range(2, 8):
a.append(sum(a[-2:]))
Here it is much more clear that we don't care about the return values from a.append. That's because that method is something we're calling to modify a in place.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am new to Haskell. I have a function as:
readData = (map read) . words
I need to match certain constraints and accordingly return data. Is there any way I can do this. Thanks.
I'm going to assume, as seems likely, this function takes a string, splits it into words, and then parses them into integers and you need those integers to be restricted to between 2 and 10,000.
So you can just wrap a check around the list you're already producing. However, to do that you're going to need to change to explicit parameters so that you can talk about them.
readData input = map read (words input)
Now we can filter it
readData input = filter (\n -> n >= 2 && n <= 10000) $ map read (words input)
So any out of range numbers just get dropped from the list.
It's best to remember that while point-free style (not using explicit parameters) can be really nice, it's often not possible to express more complex logic with it. I tend to start out by writing my functions with parameters and then taking them out if I realise I can, and if the resulting code looks easier to understand.
I am trying to draw a DAG for Longest Increasing Subsequence {3,2,6,4,5,1} but cannot break this into a DAG structure.
Is it possible to represent this in a tree like structure?
As far as I know, the answer to the actual question in the title is, "No, not all DP programs can be reduced to DAGs."
Reducing a DP to a DAG is one of my favorite tricks, and when it works, it often gives me key insights into the problem, so I find it always worth trying. But I have encountered some that seem to require at least hypergraphs, and this paper and related research seems to bear that out.
This might be an appropriate question for the CS Stack Exchange, meaning the abstract question about graph reduction, not the specific question about longest increasing subsequence.
Assuming following Sequence, S = {3,2,6,4,5,1,7,8} and R = root node. Your tree or DAG will look like
R
3 2 4 1
6 5 7
8
And your result is the longest path (from root to the node with the maximum depth) in the tree (result = {r,1,7,8}).
The result above show the longest increasing sequence in S. The Tree for the longest increasing subsequence in S look as follows
R
3 2 6 4 5 1 7 8
6 4 7 5 7 7 8
7 5 8 7 8 8
8 7 8
8
And again the result is the longest path (from root to the node with the maximum depth) in the tree (result = {r,2,4,5,7,8}).
The answer to this question should be YES.
I'd like to cite the following from here: The Soul of Dynamic Programming
Formulations and Implementations.
A DP must have a corresponding DAG (most of the time implicit), otherwise we cannot find a valid order for computation.
For your case, Longest Increasing Subsequence can be represented as some DAG like the following:
The task is amount to finding the longest path in that DAG. For more information please refer to section 6.2 of Algorithms, Dynamic programming.
Yes, It is possible to represent longest Increasing DP Problem as DAG.
The solution is to find the longest path ( a path that contains maximum nodes) from every node to the last node possible for that particular node.
Here, S is the starting node, E is the ending node and C is count of nodes between S and E.
S E C
3 5 3
2 5 3
6 6 1
4 5 2
5 5 1
1 1 1
so the answer is 3 and it is very easy to generate solution as we have to traverse the nodes only.
I think it might help you.
Reference: https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-20-dynamic-programming-ii-text-justification-blackjack/
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I'm quite sure that modern industrial strength programming languages, all read left to right
But out of the whole ecosystem of programming languages are there examples of right to left read languages ?
Apart from better alignment with human languages that written this way, would there be any advantage in such a language?
examples :
int a = 5; // read left to right
tfel ot thgir daer // ;5 = a tni
APL is a language which overcomes the inconsistencies (and difficulty in typing / parsing / executing) traditional mathematical notation by unifying certain constructs, such as:
multi-dimensional array reduction (where the reduce construct was first named as such)
+⌿2 4 6 ⍝ Sum of a list
12
×⌿2 4 6 ⍝ Product of a list
48
2 3⍴⍳6 ⍝ A 2 row, 3 column matrix
1 2 3
4 5 6
+⌿2 3⍴⍳6 ⍝ Sum down the columns
5 7 9
or the inner product:
2 3 4+.×1 0 ¯1 ⍝ Vector inner (dot) product
¯2
+/2 3 4×1 0 ¯1 ⍝ Equivalent expression for vectors
¯2
(2 2⍴1 0 1)(2 2⍴2 5 4 3) ⍝ Two matrices
┌───┬───┐
│1 0│2 5│
│1 1│4 3│
└───┴───┘
(2 2⍴1 0 1)+.×(2 2⍴2 5 4 3) ⍝ Matrix inner product
2 5
6 8
1 2 3 ∧.= 1 2 2 ⍝ Are all elements equal?
0
Since it is inspired by traditional mathematics then it follows f(g(x)):
f g x means apply g to x and then apply f to the result of that, hence right to left. f(g(x)) is also valid APL.
The easiest way to demonstrate how this affects things is:
84 - 12 - 1 - 13 - 28 - 9 - 6 - 15
70
which gives 0 in traditional mathematic execution order. With parentheses:
84 - (12 - (1 - (13 - (28 - (9 - (6 - 15)))))) ⍝ APL
70
(((((((84 - 12) - 1) - 13) - 28) - 9) - 6) - 15) ⍝ Traditional maths
0
Although it is a general purpose programming language, used for applications from finance to 3D graphics, you can see more comparisons with mathematical notation on the APL Wiki: https://aplwiki.com/wiki/Comparison_with_traditional_mathematics
And having said all that, APL is still generally "read" (by humans) basically left to right, even though it is parsed and executed "right-to-left", and even then it's more of "functions have a long right scope and short left scope". For example, using the statement separator ⋄ to have multiple statements in a single line:
2+2 ⋄ 3 4 5 ⋄ ⎕A⍳'APL'
4
3 4 5
1 16 12
The left-most statements are executed first, but each individual statement is parsed as described above.
Befunge is such a language :)
"!dlrow olleH">:#,_#
Is a standard Hello world program, however this actually evaluates left-to-right.
<v_##:<"Hello world!"
>,>>>^
This ^ evaluates right-to-left, with some up-to-down and down-to-up movement too. You can run this program at https://www.bedroomlan.org/tools/befunge-playground/#prog=hello,mode=edit .
Unefunge is a variant without up-down movement, but it is still turing-complete. If you want strictly right-to-left programs, you can write in unefunge as long as your program starts with < to send the instruction pointer in the right direction.
For more information, Stack Overflow has a befunge tag.
There was a language for children called logowriter which later got translated into hebrew as תמלילוגו, since hebrew is RTL, so was the translated language.
The תמלילוגו language was taught as part of the CS sylabus in Israeli highschools on 2008,
http://www.csit.org.il/TSTBAG/2008/899122.pdf
A bit late to the party, but now there is Avsha. That is a whole new language based on Hebrew, or ChavaScript that uses Hebrew words and translates them to JS in English (including variable names, that are translated when found in the dictionary, or written in with each letter translated to the relevant English letter).
tfel ot thgir daer // ;5 = a tni
AFAIK, there are no such languages, at least, not a serious one (thanks for tohava for the interesting example). Simply because it's mostly pointless.
You can pick any existing language, write a one liner script that reverses every line, and you have a converter (and back converter) for this "new language". You can incorporate it into your build environment - transform before compilation, reverse the lines of the compiler error messages and so on...
But really, what's the point? The only people who would benefit from this are the the ones who use right to left for their native language (BTW, AFAIK, none of them use the latin ABC). They have to learn something completely new anyways, and it makes a lot more sense to do what the rest of the world does. All the blogs, tutorial, blogs, SO, everything is written in "left-to-right" programming languages.
This question already has answers here:
How many objects are created
(4 answers)
Closed 9 years ago.
for this statement
String a="MAM"+"BCD"+"EFG"+"GFE";
How many objects will be created? (I am confused is it created 4 or 5 or 7)
Most smart compilers will realize that concatenated string constants can be concatenated at compile time. If your compiler chooses to make that optimization, the answer is one.
Otherwise, you have each of your literal strings, plus one for each concatenation. Without the optimization, the answer is 7 because you have 4 strings and 3 +es.
If you're talking about Java, the answer is one, at compile time, as specified in the JLS.
If you're not, the question is unanswerable as posed.
Only one object will be created. In this case because the plus sign is used to concatenate "string literals". :)