小编hor*_*s83的帖子

奇怪的Haskell/GHCi问题

所以我有一些代码*,当拿三分时,应该返回一个方向.我写过这个解决方案,但每次尝试运行它都会导致GHCi冻结,所以我想知道我做错了什么.这是代码:

--chapter 3 question 9
data Point x y = Point x y deriving (Eq, Show)
data Vector x y = Vector x y deriving (Eq, Show)

sub (Point x y) (Point a b) = (Vector (x-a) (y-b))
dot (Vector x y) (Vector a b) = (x*a)+(y*b)
perp (Vector x y) = (Vector (-y) x)
mag (Vector x y) = sqrt (dot v v) where v = (Vector x y)

data Direction = LeftTurn | RightTurn | Straight | Reverse …
Run Code Online (Sandbox Code Playgroud)

haskell functional-programming ghci

5
推荐指数
1
解决办法
214
查看次数

在Scala中键入参数化

所以我现在正在学习Scala,我正在尝试创建一个矢量空间为3(x,y,z坐标)的抽象矢量类.我正在尝试使用以下代码添加其中两个向量:


package math

class Vector3[T](ax:T,ay:T,az:T) {
  def x = ax
  def y = ay
  def z = az
  override def toString = "<"+x+", "+y+", "+z+">"
  def add(that: Vector3[T]) = new Vector3(x+that.x, y+that.y, z+that.z)
}
Run Code Online (Sandbox Code Playgroud)

问题是我一直收到这个错误:

错误:类型不匹配;
发现:T
必需:String
def add(即:Vector3 [T])= new Vector3(x + that.x,y + that.y,z + that.z)

我已经尝试过评论上面的"toString"方法,但这似乎没有任何影响.谁能告诉我我做错了什么?

templates scala

4
推荐指数
3
解决办法
3925
查看次数

标签 统计

functional-programming ×1

ghci ×1

haskell ×1

scala ×1

templates ×1