Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why F# compiler gets into twist with seq{0L..-5L..-10L}?

Tags:

f#

I'm having a bit of trouble declaring a descending sequence of int64.

What I want is this:

seq{0L..-5L..-10L};;

However, I get an error:

  seq{0L..-5L..-10L};;
  ---^^^^^^^^^^^^^^^

stdin(5,4): error FS0739: Invalid object, sequence or record expression

Interestingly, it works with plain int:

> seq{0..-5..-10};;
val it : seq<int> = seq [0; -5; -10]

Even more interestingly, if I put spaces between .., it starts working with int64 too:

> seq{0L .. -5L .. -10L};;
val it : seq<int64> = seq [0L; -5L; -10L]

Can someone explain why the compiler gets into the twist with seq{0L..-5L..-10L}?

like image 558
Philip P. Avatar asked Aug 31 '13 17:08

Philip P.


People also ask

Why is F-35 the best?

Designed from the ground up to prioritize low-observability, the F-35 may be the stealthiest fighter in operation today. It uses a single F135 engine that produces 40,000 lbs. of thrust with the afterburner engaged, capable of pushing the sleek but husky fighter to speeds as high as Mach 1.6.

Why the F-22 is the best?

With advanced sensors, the fighter can see threats from far away; with its stealthy shape and materials, it can sneak up on those threats undetected; with its supercruise ability, it can sneak faster than threats can react; and, with its thrust vectoring capability, it can out-dance any opponent within visual range.

Is the F-35 the best fighter jet?

All that makes the F-35 among the world's most advanced multi-role fighters flying today. Powered by F135-PW-100 engines that provide 40,000 pounds of maximum propulsion, the combat aircraft has a range of 1,200 nautical miles, and can reach speeds of upwards of Mach 1.6 (1,200 mph).

Can the F-35 fly in rain?

“Per U.S. Air Force flight regulations, pilots of all USAF aircraft — not just F-35s — are instructed not to intentionally fly into a thunderstorm,” Seal said.


1 Answers

I agree that this is a bit odd behavior. It is generally recommended (although this is not strictly required by the specification) to write spaces around .. and it works correctly in that case. So I'd recommend using:

seq { 0 .. -5 .. -10 }
seq { 0L .. -5L .. -10L }

Why is this behaving differently for int and int64? You may notice that when you write 1..-2 and 1L..-2, Visual Studio colorizes the text differently (in the first case .. has the same color as numbers, in the other case, it has the same color as .. with spaces).

The problem is that when the compiler sees 1., it may mean a floating point value (1.0) or it may be a start of 1.., so this case is handled specially. For 1L., this is not a problem - 1L. has to be the beginning of 1L...

So, if you write 1..-5..-10, the compiler uses the special handling and generates a sequence. If you write 1L..-5..-10, then the compiler parses ..- as a unary operator that is applied to 5L. Writing the spaces resolves the ambiguity between unary operator and .. followed by a negative number.

For reference, here is a screenshot from my Visual Studio (which shows 10.. in green, but .. on the second line in yellow - not particularly noticeable difference, but they are different :-))

enter image description here

like image 181
Tomas Petricek Avatar answered Oct 21 '22 10:10

Tomas Petricek