小编maf*_*ffh的帖子

你如何在Haskell中允许GADT?

今天我开始从以下网站了解GADT:haskell.orghttps://wiki.haskell.org

不幸的是,我不知道如何使用它们.如果我运行示例中的代码,我会收到以下错误:

[1 of 1] Compiling Main             ( test.hs, interpreted )

AFP_229.hs:31:1:
Illegal generalised algebraic data declaration for `Term'
  (Use GADTs to allow GADTs)
In the data declaration for `Term'
Failed, modules loaded: none.
Prelude>
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的代码:

data Term a where
  Lit    :: Int -> Term Int
  Succ   :: Term Int -> Term Int
  IsZero :: Term Int -> Term Bool   
  If     :: Term Bool -> Term a -> Term a -> Term a
  Pair   :: …
Run Code Online (Sandbox Code Playgroud)

haskell gadt

13
推荐指数
1
解决办法
2376
查看次数

在Haskell中获取Rose树的根

最近我开始学习Haskell,并且正在努力进行以下练习:

Write functions root :: Rose a -> a and children :: Rose a -> [Rose a]
that return the value stored at the root of a rose tree, respectively the children of the
root of a rose tree.

他们给了我以下基本代码:

data Rose a = a :> [Rose a]
    deriving (Eq, Show)

我不知道(:>)是什么意思.我试着通过输入ghci来理解它

Input: :t (:>)
Output: a -> [Rose a] -> Rose a
Run Code Online (Sandbox Code Playgroud)

所以我认为这意味着你有一个a值,用于Rose a从列表中查找并返回Rose a,但我仍然感到困惑,接下来要做什么.

如果我查看签名root :: Rose a -> a,该函数将如下所示: …

tree haskell

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

显示,给定查询点q,可以在时间O(log n)中测试q是否位于P内

我试图解决第6章 - 点位置"计算几何算法和应用,第3版 - 德伯格等人"一书的一些练习.不幸的是,我不知道如何解决以下练习:

Given a convex polygon P as an array of its n vertices in sorted order
along the boundary. Show that, given a query point q, it can be tested in
time O(log n) whether q lies inside P.

我的想法到目前为止: 我知道确定一个点是否位于O(log n)中的p内的唯一方法是使用有向无环图.为了使用有向无环图,我需要构建它,这在O(log n)中是不可能的.因此,我需要使用有序数组,但我所知道的唯一解决方案将花费O(N).

我希望有人可以帮助我.

algorithm geometry computational-geometry graph-algorithm

2
推荐指数
1
解决办法
779
查看次数