【问题标题】:F# Incomplete Structured Construct Caused By Type Specification类型规范导致的 F# 不完整的结构化构造
【发布时间】: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#


    【解决方案1】:

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

    let f x y : string = ...
    

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

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

    这样,string 就是y 的类型。

    对于您的具体情况,请查看此行:

    let corners shape:Shape matrixLocal:HieghtMap =
    

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

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

    【讨论】:

    • 非常感谢!对于单输入函数,我是否也应该使用括号来指定类型?
    • 是的。不管参数的数量是多少,没有括号的类型说明总是意味着函数的返回类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2015-02-06
    • 2018-07-09
    • 1970-01-01
    • 2016-10-13
    相关资源
    最近更新 更多