Haskell 如何在main中使用用户输入调用函数?

Haskell 如何在main中使用用户输入调用函数?,haskell,user-input,where-clause,main,Haskell,User Input,Where Clause,Main,Libs.hs: --TAKE INPUT OF USER CARD DETAILS AND SEPERATE EACH DIGIT AND INTO A LIST getCard :: Integer -> [Integer] getCard x | x <= 0 = [] | otherwise = lst_numb : getCard pre_numb where (pre_numb, lst_numb) = x `divMod`

Libs.hs:

--TAKE INPUT OF USER CARD DETAILS AND SEPERATE EACH DIGIT AND INTO A LIST

getCard :: Integer -> [Integer]
getCard x
    | x <= 0 = []
    | otherwise = lst_numb : getCard pre_numb
      where
        (pre_numb, lst_numb) = x `divMod` 10

--TAKE SEPERATED DIGITS LIST AND REVERSE

cardNumber :: Integer -> [Integer]
cardNumber = reverse . getCard


--DOUBLE EVERY OTHER NUMBER STARTING FROM THE RIGHT (TO THE LEFT OF CHECK NUMBER)

doubleNumber :: [Integer] -> [Integer]
doubleNumber [] = []
--doubleNumber (x:y:xs) = 2*x : y : doubleNumber xs //This is the partially working function for Q2
doubleNumber (x:y:xs)
    | validFormat (x:y:xs) == True = 2*x : y : doubleNumber xs
    |otherwise = []


--CHECKS ISSUER ID IS VALID & CARD NUMBER IS 16 DIGITS LONG

validFormat :: [Integer] -> Bool
validFormat  x
    | x == [] = False
    | (head x == 3 || head x == 4 || head x == 5 || head x == 6) && length x == 16 = True
    | otherwise = False

--ADDITION OF ALL DIGITS INSIDE THE LIST
addNumbers :: [Integer] -> Integer
addNumbers xs = sum xs

--SUBTRACT NINE FROM DOUBBLE DIGIT NUMBERS

subDoubles :: [Integer] -> [Integer]
subDoubles [] = []
subDoubles (x:xs)
    | x > 9 = x - 9 : subDoubles xs
    | otherwise =  x : subDoubles xs

--COMBINED FUNCTION TO VALIDATE A CREDIT CARD NUMBER

validateCard :: Integer -> Bool
validateCard x = compute x `mod` 10 == 0
    where
      compute :: Integer -> Integer
      compute = addNumbers . subDoubles . doubleNumber . cardNumber

--DISPLAY USER MSG IF CARD IS VALID OR NOT
isValid card
  | card == True = "This is a valid credit card!"
  | otherwise = "This card is invalid"
——输入用户卡的详细信息,并将每个数字分隔成一个列表
getCard::Integer->[Integer]
getCard x
|x[整数]
卡号=反向。getCard
--从右边开始每隔一个数字翻倍(支票号码的左边)
doubleNumber::[Integer]->[Integer]
双倍数字[]=[]
--doubleNumber(x:y:xs)=2*x:y:doubleNumber xs//这是Q2的部分工作函数
双倍数字(x:y:xs)
|有效格式(x:y:xs)=True=2*x:y:doubleNumber xs
|否则=[]
--检查发卡机构ID是否有效以及卡号是否为16位
有效格式::[Integer]->Bool
有效格式x
|x==[]=False
|(头部x==3 | |头部x==4 | |头部x==5 | |头部x==6)和长度x==16=True
|否则=假
--添加列表中的所有数字
addNumbers::[Integer]->Integer
addNumbers xs=sum xs
--从双位数中减去九
子双精度::[Integer]->[Integer]
子双精度[]=[]
次双精度(x:xs)
|x>9=x-9:subDoubles xs
|否则=x:子双精度x
--用于验证信用卡号的组合函数
validateCard::Integer->Bool
validateCard x=compute x`mod`10==0
哪里
计算::整数->整数
计算=添加数字。替身。双号。卡号
--如果卡有效或无效,则显示用户消息
这张卡有效吗
|card==True=“这是一张有效的信用卡!”
|否则=“此卡无效”
Main.hs:

main :: IO ()
main = do
    putStrLn "Please enter a credit card number:"
    input <- getLine
    let card = read input :: Integer
    isValid . validateCard card
main::IO()
main=do
putStrLn“请输入信用卡号码:”

inputRobin Zigmond在他的评论中说了这一点,但您正在寻找
getLine

getLine :: IO string
main
中,您可以这样使用它:

main = do
    input <- getLine
main=do

输入您在以下内容中混淆了优先规则:

isValid . validateCard card
这解析为

isValid . (validateCard card)
但这不起作用,因为
validateCard
不是一个函数。您需要编写以下内容之一:

isValid . validateCard $ card
isValid $ validateCard card

使用
getLine
获取用户输入,然后在此基础上运行函数。您需要先将其从字符串转换为整数文件。您的最后一行
isValid.card@RobinZigmond我为main.hs上载了错误的代码,这是我最初尝试的方式,但仍然存在issues@PaulJohnson抱歉这不是我想上传的,此编辑的帖子在匹配预期类型时出错。如果要关闭,帖子缺少焦点。我可以使用getLine获取用户输入,但是我不知道如何将
input
与我的函数
validateCard
一起使用,因为
isValid
返回字符串,这是无效的。由于
isValid
的输出似乎是针对用户的消息,因此最自然的做法是执行
putStrLn$isValid$validateCard
这是一个合理的猜测,但我只是处理了这个表达式,因为我不确定如何处理结果。当然,你是对的,必须用它做点什么。