简体   繁体   English

从haskell的列表中找到最小偶数

[英]find minimum even number from a list in haskell

Please help me write a function in haskell which would help me find a minimum even number in a list. 请帮助我在haskell中编写一个函数,该函数将帮助我在列表中找到最小偶数。 Incase the list contains all the odd number throw an exception. 如果列表包含所有奇数,则抛出异常。 I am able to write two separate functions but not able to write one complete program. 我能够编写两个单独的函数,但不能编写一个完整的程序。 My code is as follows. 我的代码如下。

retainEven :: [Int] -> [Int]
retainEven [] = []
retainEven (n:ns)= 

    if ((mod n 2) == 0)
        then n : (retainEven ns) 
    else retainEven ns


enter code here




mymin [] = error "no element" 
mymin [x] = x  
mymin (x:y:xs) = mymin ((if x < y then x else y):xs)

过滤偶数元素,然后取最小值:

minEvens = minimum . filter even

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM