当前位置:  开发笔记 > 编程语言 > 正文

在Haskell中创建一个简单的数组

如何解决《在Haskell中创建一个简单的数组》经验,为你挑选了1个好方法。



1> CR Drost..:

就在这里.您使用模式匹配:

-- type signature can be slightly more general: any list of equatables.
isaprefix :: (Eq a) => [a] -> [a] -> Bool

-- first are the two "boring" cases.
-- we define that empty lists are prefixes of everything.
isaprefix [] _ = True

-- then: we also define that you can't be the prefix of an empty list.
isaprefix _ [] = False

-- those 2 are technically in conflict, so you need to know that the empty list is defined,
-- by the order of those definitions, to be a prefix of the empty list.  This supports the
-- general property that `isprefixof x x == True` for all `x`.

-- ok, now here's the more interesting pattern: both lists are nonempty. We need them to
-- match on their first elements and also for the rest of the first list to prefix the 
-- rest of the second list.
isaprefix (a:as) (b:bs)
    | a == b     = isaprefix as bs
    | otherwise  = False

推荐阅读
依然-狠幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有