列表中的序列号haskell [英] Sequential numbers in a list haskell
问题描述
我是haskell的新手,我尝试了一些我以前为java完成的编码问题,但是下面的问题让我很难过。
基本上这个想法是编写一个包含整数列表([Int])的函数来确定一个整数列表中是否有连续的1。例如,输出如下:
输入:func [0,0,1,1,0]
输出:True
这个问题在haskell中的示例解决方案将不胜感激,
谢谢
一种方法是使用模式匹配查找列表头部的连续列表,并向下推进列表直到找到它们,或者用尽元素查看。
consecutiveOnes [] = False
consecutiveOnes(1:1:_)= True
consecutiveOnes(_:xs)= consecutiveOnes xs
I am new to haskell and I was attempting a few coding problems that I previously completed for java, however the following problem has me stumped.
Basically the idea is to write a function that takes in a list of integers ([Int]) establishes whether a list of integers has consecutive 1's within it. For example the output of the following would be: Input: func [0,0,1,1,0] Output: True
A sample solution for this problem in haskell would be greatly appreciated, Thanks
One way would be to use pattern matching to look for consecutive ones at the head of the list, and advance down the list until you find them, or you run out of elements to look at.
consecutiveOnes [] = False
consecutiveOnes (1:1:_) = True
consecutiveOnes (_:xs) = consecutiveOnes xs
这篇关于列表中的序列号haskell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!