开发者

F# - This code isn't compiling for me

开发者 https://www.devze.com 2023-01-02 17:54 出处:网络
This code isn\'t compiling for me:let countDown = [5L .. −1L .. 0L];; I have a book (page 33) that says it should return this:

This code isn't compiling for me: let countDown = [5L .. −1L .. 0L];;

I have a book (page 33) that says it should return this:

val countDown : int list = [5L; 4L; 3L; 2L; 1L; 0L]

Compiler Error:

Program.fs(42,24): error FS0010: Unexpected character '−' in expression
>
> let countDown = [5L .. −1L .. 0L];;

  let countDown = [5L .. −1L .. 0L];开发者_开发知识库;
  -----------------------^

The book's wrong. but why? is it an update to the language? what is the way to achieve that?

Edit: the problem was that the character copied from the PDF, isn't the - character.


Your original code works fine for me even without the modifications that Igor suggested:

Microsoft (R) F# 2.0 Interactive build 4.0.30319.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

> let l = [ 10L .. -1L .. 0L ];;
val l : int64 list = [10L; 9L; 8L; 7L; 6L; 5L; 4L; 3L; 2L; 1L; 0L]

A possible subtle error is that if you (for example) pasted the code from Word (or some other program), it may have replaced the - symbol with some other type of dash that looks the same, but has actually a different code.

Another way to break the code is to remove some spaces - for example, there must be a space between .. and -1L. Otherwise, I don't see any reason why it shouldn't work.


Try this:

let countDown = [5L .. (-1L) .. 0L];;

Or this:

let countDown = [5 .. -1 .. 0];;

Both of the above will work.

Here is some output:

> let countDown = [5 .. -1 .. 0];;

val countDown : int list = [5; 4; 3; 2; 1; 0]

> let countDown = [5L .. (-1L) .. 0L];;

val countDown : int64 list = [5L; 4L; 3L; 2L; 1L; 0L]
0

精彩评论

暂无评论...
验证码 换一张
取 消