F#类型规范引起的不完整结构化构造

时间:2017-10-10 17:23:51

标签: f#

所以,我有一个函数corners,我想要一个2D数组(缩写类型为HieghtMap)并返回一个记录类型的坐标列表。最初,我没有指定matrixLocal的类型,这导致了 System.Exception: Operation could not be completed due to earlier error  The type 'matrixLocal' is not defined. at 30,28

现在我正在指定类型,我收到了这个新错误

 Syntax error in labelled type argument at 30,39 
 Incomplete structured construct at or before this point in
 interaction. Expected incomplete structured construct at or before
 this point, ';', ';;' or other token.

我相信这是由于farside(因为它甚至不能自己发挥作用),但我不知道为什么,因此我在这里。我发现的关于后一个错误的其他问题在这种情况下似乎不适用(一个是关于缩进,另一个是关于尝试在循环中重新定义变量)。

守则:

module DiamondSquare =

//create type for defining shapes
///Defined by length of the side of a square that the ovject is inscribed in
type Shape =
    | Square of int
    | Diamond of int

///the X and Y position
type Coordinates = {X: int; Y: int}

///The Hieghtmap of a given chunk of region as a series of floats that are the offset from the base hieght
//was HieghtMap = HieghtMap of float[,], but was changed so that any 2D float array would be accepted
type HieghtMap = float[,]

//Create matrix of zeroes of chunk size to initilize this variable
let matrix = Array2D.zeroCreate<float> 9 9

//locate center of shape
//  since each shape is a square, or can be inscribed within one, pass it a matrix and find the
//  coordinate of the center (same value for i and j)
///Finds center of shape inscribed within a square. Takes a matrix, returns coordinates for within the matrix
let locateCenterpoint matrixLocal = 
    let coord = int ((Array2D.length1 matrixLocal) - 1) / 2
    {X = coord; Y = coord;}

//locate corners of a shape that is inscribed in a square
///Returns list of corner values for a given shape. Takes a matrix and returns a list of Coordinates
let corners shape:Shape matrixLocal:HieghtMap =
    let farSide = Array2D.length1 matrixLocal - 1
    let getSquareCorners = 
        {X = 0; Y = 0}::{X = farSide; Y = 0}::{X = 0; Y = farSide}::{X = farSide; Y = farSide}::[]
    let getDiamondCorners =
        {X = farSide / 2; Y = 0}::{X = farSide; Y = farSide / 2}::{X = farSide / 2; Y = farSide}::{X = 0; Y = farSide / 2}::[]
    match shape with
    | Square -> getSquareCorners
    | Diamond -> getDiamondCorners
    | _ -> None

1 个答案:

答案 0 :(得分:1)

在F#中定义值时,第一个冒号表示值名称的结尾和类型声明的开头。例如:

let f x y : string = ...

在此声明中,string是函数的返回类型,而{em>不是类型的y参数。要将类型声明应用于列表中的单个值,请使用括号:

let f x (y: string) = ...

这样,string就是y的类型。

针对您的具体情况,请看这一行:

let corners shape:Shape matrixLocal:HieghtMap =

看看问题是什么? Shape被解析为corners函数的返回类型,这使得后续matrixLocal:HeightMap无意义。要修复,请应用括号:

let corners (shape:Shape) (matrixLocal:HieghtMap) =
相关问题