【发布时间】:2017-10-11 01:38:22
【问题描述】:
所以,我有一个函数corners,我想获取一个二维数组(具有缩写类型 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
【问题讨论】:
标签: f#