【问题标题】:I couldn't figure out how to use sInsert function. Can you help me? [closed]我不知道如何使用 sInsert 函数。你能帮助我吗? [关闭]
【发布时间】:2021-05-07 12:06:11
【问题描述】:

我不知道如何使用sInsert 函数。你能帮帮我吗?

sInsert :: Ord a => a -> Tree a -> Tree a
sInsert x Nil = Node x Nil Nil
sInsert x (Node a t1 t2) = if x <= a
                        then Node a (sInsert x t1) t2
                        else Node a t1 (sInsert x t2)

sInsert (???)

【问题讨论】:

标签: haskell


【解决方案1】:

您可以从Nil 开始构造一棵树,然后在其中插入数据。例如,这里有一小部分您可以尝试在 GHCi 中求值的表达式:

sInsert "c" Nil
sInsert "a" (sInsert "c" Nil)
sInsert "b" (sInsert "a" (sInsert "c" Nil))
...

【讨论】:

  • 我有错误 | 51 |打印(s插入“c”无)| ^^^^^^^^^^^^^^^^^^^^^^
  • @ColibV 您是否使用deriving Show 之类的方式声明了您的树类型?否则无法打印结果。
  • 这是我的树类型声明:data Tree a = Node a (Tree a) (Tree a) | Nil 导出 Eq
  • 我已经编辑了我的类型声明,你的例子已经开始工作了。谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-04-18
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-19
  • 2022-07-07
  • 1970-01-01
相关资源
最近更新 更多