You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

F#报FS0001类型错误:自定义figure类型move函数调用异常问题

F# move函数类型错误问题修复

错误根因

  • 你的move函数是柯里化定义,需要依次传入figure实例、位移向量两个参数。你在处理Mix分支的递归调用时,只给move传了第一个参数f1/f2,没有传入位移向量v,因此move(f1)的返回值是等待接收向量参数的函数(类型为int * int -> figure),而非你预期的figure类型,这就是编译器报错的核心原因。
  • 额外小问题:point是int*int的类型别名,不需要作为构造器调用,直接写元组即可;另外你Circle分支的y坐标逻辑写反了,用了cy-vy,如果没有特殊需求应该和矩形保持一致用cy+vy。

修正后的完整代码

type point = (int*int)
type figure =
 | Circle of point * int * string
 | Rectangle of point * point * string
 | Mix of figure * figure 

let circ = Circle((50,50),45,"Red")
let rect = Rectangle((40,40),(90,110),"Blue")
let figTest : figure = Mix(circ,rect)

let rec move figure (v:int*int) : figure= 
 let vx = fst v
 let vy = snd v
 match figure with
 | Circle ((cx,cy) , radius, colour) -> 
     // 修正y坐标计算逻辑,移除多余的point构造调用
     Circle((cx + vx, cy + vy), radius, colour)
 | Rectangle((x0,y0), (x1,y1), colour) -> 
     Rectangle((x0 + vx, y0 + vy), (x1 + vx, y1 + vy), colour)
 | Mix(f1,f2) ->
     // 递归调用时补全位移向量参数
     let newF1 = move f1 v
     let newF2 = move f2 v
     Mix(newF1, newF2)

调用示例

调用时需要同时传入figure实例和位移向量,比如沿x+10、y+20移动:

let movedFig = move figTest (10,20)

内容的提问来源于stack exchange,提问作者Zuper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 17:54:05