Haskell 关于模式匹配的问题

发布于 2024-09-25 23:21:17 字数 517 浏览 4 评论 0原文

我正在尝试编写一个函数,该函数接受一个列表,如果按排序顺序返回 true,如果不是按排序顺序则返回 false:

到目前为止,我所拥有的是:

myordered [] = True
myordered [x] = True
myordered list1
 | (head list1) <= (head (tail list1)) = myordered(tail list1)
 | otherwise                           = False

根据我们的分配,所有头和尾操作应写为“ x:xs”类型语法。

我对带有警卫的部分的翻译是:

myordered y:x:xs
 | (y) <= (x) = myordered(xs)
 | otherwise  = False

本质上这个问题可以归结为: 如何用“x:xs”类型语法表达 (head (tail list1)) ?

干杯, -紫谷

I'm trying to write a function that takes in a list and returns true if it is in sorted order and false if not:

So far what I have is:

myordered [] = True
myordered [x] = True
myordered list1
 | (head list1) <= (head (tail list1)) = myordered(tail list1)
 | otherwise                           = False

Based on our assignment, all head and tail operations should be written down as "x:xs" type syntax.

the translation I come up with for the section with a guard is:

myordered y:x:xs
 | (y) <= (x) = myordered(xs)
 | otherwise  = False

Essentially this question boils down to:
How do you express the (head (tail list1)) in "x:xs" type syntax?

Cheers,
-Zigu

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

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

发布评论

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

评论(3

马蹄踏│碎落叶 2024-10-02 23:21:17

您的模式几乎是正确的,您只需要用括号将其括起来:

myordered (y:x:xs)

另请注意,在 y <= 中不需要用括号将 yx 括起来x。

另外,您的第二个版本中存在语义错误:

myordered(xs) 这里 xs 指的是 tail 的尾部,但您想要整个尾部,所以您应该这样做 myordered (x:xs) 或者:其中

myordered (y:xs@(x:_))
 | y <= x = myordered xs
 | otherwise  = False

表示:xs 是该列表的尾部,x 是该尾部的头部,而 _(被忽略)是尾部的尾部。

Your pattern is almost correct, you just need to surround it with parentheses:

myordered (y:x:xs)

Also note that there's no need to surround y and x with parentheses in y <= x.

Also there's a semantic mistake in your second version:

myordered(xs) here xs refers to the tail of tail, but you want the whole tail, so you should do myordered (x:xs) or alternatively:

myordered (y:xs@(x:_))
 | y <= x = myordered xs
 | otherwise  = False

Which says: xs is the tail of that list, x is the head of that tail, and _ (which is ignored) is the tail of the tail.

攀登最高峰 2024-10-02 23:21:17

借助 Data.List 中提供的 zipWith 函数的另一种方法来实现此目的怎么样?

 myordered xs= and $ zipWith (<=) xs (tail xs)

zipWith 函数接受两个列表并应用一个函数。这里它会根据条件返回一个布尔数组。
and 接受布尔值列表,仅当列表中的所有值均为 True 时才返回 True

How about another way to do this with the help of zipWith function available in Data.List

 myordered xs= and $ zipWith (<=) xs (tail xs)

zipWith function takes two list and apply a function. Here it will return an array of boolean according to the condition .
and takes a list of boolean values and returns True only if all the values in the list are True

脸赞 2024-10-02 23:21:17

怎么样:

isordered :: [Int] →  Bool
isordered [] = True
isordered xs = foldl (&&) True $ zipWith (<=) xs $ tail xs

哦,只是为了好玩

isordered :: [Int] →  Bool
isordered [] = True
isordered (x:xs) = (foldl comp (Just x) xs) /= Nothing
   where comp (Just a) b = if a ≤ b then Just b else Nothing
         comp Nothing _ = Nothing  

How about

isordered :: [Int] →  Bool
isordered [] = True
isordered xs = foldl (&&) True $ zipWith (<=) xs $ tail xs

Oh, and just for fun:

isordered :: [Int] →  Bool
isordered [] = True
isordered (x:xs) = (foldl comp (Just x) xs) /= Nothing
   where comp (Just a) b = if a ≤ b then Just b else Nothing
         comp Nothing _ = Nothing  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文