列表中的序列号haskell [英] Sequential numbers in a list haskell

查看:73
本文介绍了列表中的序列号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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆