检查清单可分性

时间:2014-02-26 20:02:14

标签: haskell

如何检查列表是否可被9整除

到目前为止我试过

module Main where

main :: IO ()
main =  do   
        let line = [23,24, 30,35,36,40,42,44,54]
        putStrLn $ listofints line   

listofints :: [int] -> [int]  
listofints name = [x | x <- name, n `mod` x == 0]

我到处都是错误,我对haskell很新。

如果列表中包含偶数整数并将其从列表中删除,请帮助我查看如何写入检查。

谢谢

3 个答案:

答案 0 :(得分:3)

假设您要在line中打印任何可被9整除的数字,您可以使用filter

main :: IO ()
main =  do   
        let line = [23,24, 30,35,36,40,42,44,54]
        print $ listofints line  

listofints :: [Int] -> [Int]
listofints = filter (\x -> x `mod` 9 == 0)

答案 1 :(得分:3)

按功能

执行此功能
main :: IO ()
main =  do   
        let line = [23,24, 30,35,36,40,42,44,54]
        putStrLn $ listofints line

putStrLn适用于字符串,它不像python或类似的通用“print”。为此,我们有print:)

let line = ...
print $ listOfInts line

转到下一个

listofints :: [Int] -> [Int] -- upper case  
listofints name = [x | x <- name, x `mod` n == 0]

您没有为n提供值,因此这是一个错误。尝试改为

 listofints name = [x | x <-name, x `mod` n == 0]
     where n = ???

在哪里???是您想要n的值,否则您可以内联它并获得[..., x `mod` ??? == 0]

答案 2 :(得分:-1)

module Divison where
divisibility :: [Int] -> [String]
divisibility (x:xs) = map show([if x `mod` 9 == 0 && x `mod` 3 == 0 then "divideby3and9"
else if x `mod` 9 == 0 then "divideby9"
else if x `mod` 3 == 0 then "divideby3" else show(x) | x <- x:xs]