2017-10-10 71 views
1

所以,我有一個函數corners,我想要一個二維數組(帶有縮寫類型的HieghtMap)並返回記錄類型的座標列表。起初,我不指定matrixLocal的類型,這導致 System.Exception: Operation could not be completed due to earlier error  The type 'matrixLocal' is not defined. at 30,28F#不完整的類型結構引起的結構化構造

現在,我指定的類型,我得到這個新的錯誤

 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

當定義在F#的值,則第一個冒號表示值名稱和類型聲明的起動結束。例如:

let f x y : string = ... 

在此聲明,string是返回類型的功能,和不類型y參數。爲了類型聲明應用到列表中的一個值,使用括號:

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

這樣,stringy類型。

爲了您的具體情況,看看這個行:

let corners shape:Shape matrixLocal:HieghtMap = 

看到的問題是什麼? Shape正在被解析爲corners函數的返回類型,並且這會使後續的matrixLocal:HeightMap變得無意義。要修復,請使用圓括號:

let corners (shape:Shape) (matrixLocal:HieghtMap) = 
+0

非常感謝!對於單輸入函數,我是否也應該使用括號來指定類型? –

+0

是的。無論參數的數量如何,沒有括號的類型規範總是意味着函數返回類型。 –