显示哈斯克尔的结果

发布于 2025-02-06 15:59:41 字数 183 浏览 0 评论 0原文

我是Haskell的新手。

如何返回(x1,x2)并从我的代码中打印出来?

qqq x
   | x < 0  x1 = mod (-x) 10 
   | 1 < x && x < 99 x1 = mod x 10
   | x2 = mod x 10

I'm very new to haskell.

How can I return (x1,x2) and print it out from my code?

qqq x
   | x < 0  x1 = mod (-x) 10 
   | 1 < x && x < 99 x1 = mod x 10
   | x2 = mod x 10

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煞人兵器 2025-02-13 15:59:41

您以错误的方式使用守卫。您似乎将它们视为如果语句,则可以将其用于 atsignement 。在Haskell中,您不将分配值为变量,您 neclare 这些。您可以使用:

qqq :: Integral a => a -> (a, a)
qqq x
   | x < 0 = (mod (-x) 10, x2)
   | 1 < x && x < 99 = (mod x 10, x2)
  where x2 = mod x 10

因此,每个后卫在方程符号之前都有条件(=),在右侧,返回了2件托盘,将第一个项目返回x1 ,作为第二项x2

您还应该实现x == 1x&gt; = 99的额外案例。

You are using guards the wrong way. You seem to see these as if statements, that you then can use for assignements. In Haskell, you do not assign values to a variable, you declare these. You can work with:

qqq :: Integral a => a -> (a, a)
qqq x
   | x < 0 = (mod (-x) 10, x2)
   | 1 < x && x < 99 = (mod x 10, x2)
  where x2 = mod x 10

Here each guard thus has a condition before the equation sign (=), and at the right side returns a 2-tuple with as first item an expression for x1, and as second item x2.

You should also implement extra case(s) for x == 1 and x >= 99, these are not covered by the two guards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文