使用详细语法在F#嵌套for循环中编译错误

时间:2012-08-31 16:51:38

标签: for-loop f# functional-programming nested visual-studio-2012

我是F#的新手,但是当我使用详细语法时 在另一个中放一个for循环, 它不会编译:

#light "off"

let Multiple9x9 () = 
  for i in 1 .. 9 do
    printf "\n";
    for j in 1 .. 9 do
      let k = i * j;
      printf "%d x %d = %2d " i j k ;
    done;;
  done;;
Multiple9x9 ();;
printf "\n" ;;

我知道它适用于#light“on”:

let Multiple9x9 () = 
  for i in 1 .. 9 do
    printf "\n"
    for j in 1 .. 9 do
      let k = i * j
      printf "%d x %d = %2d " i j k
Multiple9x9 ()
printf "\n"

1 个答案:

答案 0 :(得分:6)

如果您要使用详细语法,则在每次in绑定后需要let。此外,在内循环中;;之后您不需要done:它会使外循环不完整(切断外部done)。

以下是更正后的代码:

let Multiple9x9 () = 
  for i in 1 .. 9 do
    printf "\n";
    for j in 1 .. 9 do
      let k = i * j in
      printf "%d x %d = %2d " i j k;
    done;
  done;;
Multiple9x9 ();;
printf "\n" ;;