简体   繁体   中英

F# - This code isn't compiling for me

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM