Why do Clojure vector function results exclude the stop value? [closed] - vector

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Forgive me for the newb question and potentially incorrect terminology.
Clojure vector functions produce values that do not include the stop value. For example:
=> (subvec [:peanut :butter :and :jelly] 1 3)
[:butter :and]
=> (range 1 5)
(1 2 3 4)
The doc for range explicitly states this but doesn't give a rational: "...Returns a lazy seq of nums from start (inclusive) to end (exclusive)...".
In Ruby these operations are inclusive:
(1..5).to_a
=> [1, 2, 3, 4, 5]
[:peanut, :butter, :and, :jelly][1,3]
=> [:butter, :and, :jelly]
Obviously these are very different languages, but I'm wondering if there was some underlying reason, beyond a personal preference by the language designers?

Making the end exclusive allows you to do things like specify (count collection) as the endpoint without getting an NPE. That's about the biggest difference between the two approaches.

It might be that the indexing was chosen in order to be consistent with Java libraries. java.lang.String.substring and java.util.List.subList both have exclusive-end indexes.

Related

How can we correctly determine if two floating point variables are equal? [closed]

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 2 years ago.
Improve this question
I'm doing a quiz and I wonder if I can correctly determine it. These are options:
See if a == b is true
Check if a - b == 0 is true
Check if ((a-b)*(a-b))/(a*a + b*b) is close to or is zero
Check a*b == a*a is true
None of the above
I tried to compare float and double values in C++ and I didn't succeed with any of the given formulas. I'd say "None of the above" but I don't know how it works in other languages. I read somewhere that in Java could be different but I would choose the last options as generally other answers are incorrect. What do you think?
The floating-point comparison operation is “perfect” in that it returns true if and only if the tested relation is true. There are never any rounding errors. There is incorrect folklore that you cannot compare floating-point numbers or need to use some tolerance to compare them, but this is because earlier operations that prepare the operands to a comparison typically have rounding errors, similar to the way 7/3 rounds to 2 in integer arithmetic.
If your quiz is based on correct principles of floating-point arithmetic, then “See if a == b is true” is correct. Otherwise, the answer is unclear and the quiz is suspect.

Prolog, produce lists with m first members reversed [closed]

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 2 years ago.
Improve this question
I have a trouble finding a solution to the following problem in Prolog.
Say we have a list with N+1 members (i.e. [X1, X2, X3, ..., XN, X(N+1)]). We want to write a predicate (with recursion) in prolog that produces all possible lists which have reversed the first m of their members, with m taking values 2<=m<=N. For example if we have the list [1,2,3,4,5,6], then the predicate should return the lists:
[2,1,3,4,5,6], [3,2,1,4,5,6], [4,3,2,1,5,6], [5,4,3,2,1,6].
The predicate must have the form
move(List1, List2):-
Where List1 is the original list and List2 is the resulted list after the reversal of m first members.
Any help is highly appreciated.
Thanks in advance
Shouldn't be more difficult than
reverse_n(Xs,N,Ys) :- % reverse the first N of Xs to form Ys:
length(Pfx,N), % - construct a list of unbound vars of length N
append(Pfx,Sfx,Xs), % - partition the source list into a Pfx and Sfx
reverse(Pfx,Rev), % - reverse the prefix
append(Rev,Sfx,Ys). % - Finally, glue it back together

Choosing the appropriate coding expression for a given job [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Common Lisp provides many flexible coding options for achieving a given result. However, it is sometimes difficult to choose the best approach. For example, the following vector expressions all produce the same result in different ways.
(defparameter objects (list 1 2 3))
(apply #'vector objects)
(coerce objects 'vector)
(make-array (length objects) :initial-contents objects)
(read-from-string (format nil "#~S" objects))
Of course, some expressions are more flexible than others, depending on the required output; but for a given output as above, what criteria are useful for deciding which to use?
(apply #'vector objects) is subject to the usual limitations of APPLY, which is that objects shouldn't hold more than CALL-ARGUMENTS-LIMIT elements. This is bad style even when you have only a few arguments.
COERCE is great: not only it performs the job, it also conveys the intent very well. However, you won't be able to give additional parameters for the resulting vector (e.g. fill-pointer, etc.); you cannot convert nested lists into matrices.
MAKE-ARRAY gives you full control over the resulting array: adjustability, fill-pointer, dimensions, element-type, displacement.
READ-FROM-STRING is a big no for data conversion, in general. In terms of useless computations, this approach is the Rube Goldberg's version of coerce. It also comes with a lot of security concerns, unless you are 100% sure about what the string contains. Here, you create the string yourself, but if your data contains any value for which another part of the code redefined PRINT-OBJECT, the code might break.

Two method arguments or a new class.. for (X and Y cords) [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
In the book 'Clean Code' by Robert C. Martin, he recommends making use of:
Circle makeCircle(Point center, double radius);
over
Circle makeCircle(double x, double y, double radius);
He basically argues that making a class to avoid using multiple method arguments is preferred.
What is your opinions on this? Please explain to me the benefits, or disadvantages of either.
There are a few reasons for this.
First, it helps to group parameters meaningfully. In this trivial example, it's immediately obvious that x and y go together, but it might not be as immediately obvious when dealing with a more obscure example.
Perhaps more importantly, it cuts down on having too many parameters to keep track of meaningfully; once you have a method that takes more than 3 or 4 parameters, it gets more and more cumbersome to keep track of which parameter is which. Binding parameters together in a class or struct helps avoid that.
Consider this example:
int HowManyMinutesToAirport(int AirportIdentifier, string AirportName, int PlainIdentifier, string PlainName, int PlaneSpeed, string PlaneCompassDirection);
vs.
int HowManyMinutesToAirport(Airport airport, Plane plane);

Why does my calculator return true for 2^34 == 2^34 - 1? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm asking this question here because I believe this is more of a programmatic issue than anything else.
I'm using a TI-84 Plus Sliver Edition calculator that contains logical operators that can test for less-than, greater-than, and equality. I find that when I input the expression:
2^34 == 2^34 - 1
it gives me a surprising 1 for true. It's worth mentioning that my calculator can't precisely output the result of 2^34. Rather, it uses the exponential notation for it and any other powers greater than 33. Is this a potential factor in the boolean output?
Furthermore, the equality test only returns true if the second expression is subtracting by 1 to 9. When the number is >= 10 it then correctly returns false.
Could it be a rounding error? Why is this expression returning true?
Your calculator cant keep track of numbers that large.
Every calculator has a set level of precision (lets say 10 digits). Every answer the calculator gives is rounded so that the answer has that many digits, then it shifts the decimal place as much as it needs to in order to make the number big or small (in your case very large).
Your number is so large that when you subtract 1, that causes a change after the 10th digit. This then gets rounded back to what you started with, and then compared. So naturally, it thinks they are the same number (to the precision it is capable of).

Resources