是否可以在haskell中存储删除的值

是否可以在haskell中存储删除的值,haskell,Haskell,您好,我是haskell的新手,我只是想知道是否可以存储已删除的值: 这是我的密码 input :: Integer -> String input x = checklength $ intolist x intolist 0 = [] intolist x = intolist (x `div` 10) ++ [x `mod` 10] checklength x = if length(x) >= 13 && length(x) <= 16 then

您好,我是haskell的新手,我只是想知道是否可以存储已删除的值:

这是我的密码

input :: Integer -> String
input x = checklength $ intolist x

intolist 0 = []
intolist x = intolist (x `div` 10) ++ [x `mod` 10] 

checklength x = if length(x) >= 13 && length(x) <= 16 then doubleall 
(init(x)) else "Not valid length of credit card number" 

doubleall x = finalcheck $ final $ double (reverse (x))

double x = case x of
[]   -> []
[x]  -> if (x*2 < 10) then [x*2] else [x*2 `div` 10 + x*2 `mod` 10]
x:y:xs -> (if (x*2 < 10) then [x*2] else [x*2 `div` 10 + x*2 `mod` 10]) ++ 
y:double xs

final x = (sum x) * 9

finalcheck x = if (x `mod` 10 == ...... ) then "True" else "False"  
input::Integer->String
输入x=检查长度$intolist x
列表0=[]
输入列表x=输入列表(x`div`10)+[x`mod`10]
检查长度x=如果长度(x)>=13&&length(x)[]
[x] ->如果(x*2<10),那么[x*2]else[x*2`div`10+x*2`mod`10]
x:y:xs->(如果(x*2<10),则[x*2]否则[x*2`div`10+x*2`mod`10])++
y:双X
最终x=(总和x)*9
finalcheck x=如果(x`mod`10==…),则为“真”,否则为“假”
我的代码基本上将输入作为整数,例如987564736264535。然后将这个整数变成一个数字列表,如[9,8,7..5]。然后检查长度是否在13到16位之间。如果不是,您将得到一个错误声明。如果数字在所需数量之间,它将进入doubeall函数,并使用(init)删除最后一个数字删除的数字为5,其中数字将翻倍并颠倒列表顺序。然后将这些数字相加并乘以9。最后一步,我已经做了一部分,是采取数字的最后一位,已经被加在一起,乘以9。让我们举个例子,假设我得到456,然后我用mod 10取最后一个数字,也就是6**现在我遇到了一个问题,我想检查这个6是否与我使用init时在checklength函数中删除的数字相同。因此,在checklength函数中,我删除了数字5**


谢谢

删除数据后,您将无法再次访问它。您需要一个保留要剥离的最终校验位的函数

由于顺序(大部分)不相关,请考虑:

validate :: Integer -> Bool
validate x = let digits = toDigits x
             in  if   checkLength digits
                 then doesMatch . splitCheckdigit $ digits
                 else False
  where
  toDigits 0 = [0]
  toDigits x = go x
    where
    go 0 = []
    go x = let (d, m) = x `divMod` 10
           in  m : toDigits d
  -- reverses order
  checkLength x = let l = length x
                  in  13 <= l && l <= 16
  splitCheckdigit (checkdigit:rest) = (checkdigit, rest)
  -- remember we reversed in toDigits, so the *first* digit is the checkdigit!
  doesMatch (checkdigit, rest) = let total    = (*9) . sum . reduce $ rest
                                     shouldBe = total `mod` 10
                                 in  checkdigit == shouldBe
    where
    reduce (x:y:xs) = (sum . toDigits $ x) : y : reduce xs
    reduce [x]      = [sum . toDigits $ x]
    reduce []       = []
    -- note how @toDigits@ is reused here rather than redefined.

试着编辑你的问题,使你的逻辑解释更具可读性;正如所写的,它非常乏味。您是否在问如何定义一个函数
::[a]->([a],a])
,该函数同时返回init和列表的最后一个元素?程序的工作方式是存储一个值,然后使用init从该值中删除一个数字。现在,随着我们对程序的深入,其他事情也会发生,比如加倍元素并添加它们。最后,我再次使用mod后,所有的翻倍和添加等已经完成。我想检查我所取的mod编号是否与我最初在程序中删除编号时的编号相同。然后,如果删除的原始编号等于最终的模块编号,则我要打印true else false
finalcheck
没有它需要的元素。所以你需要解决这个问题。很可能,您应该将“removed”元素作为单独的参数传递给它。
toDigits >>> ((doesMatch <<< splitCheckdigit) &&& checkLength) >>> uncurry (&&)