Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试检查haskell中除原始数字的数字_Haskell_Modulo_Tail_Digits_Head - Fatal编程技术网

尝试检查haskell中除原始数字的数字

尝试检查haskell中除原始数字的数字,haskell,modulo,tail,digits,head,Haskell,Modulo,Tail,Digits,Head,Haskell代码问题 描述:代码应该返回数字中的多少位数除以整个数字。 例如,12有两个数字[1,2],这两个数字都是除2(12%2和12%1都是0),因此返回2,因为有两个数字将数字除。 对于102,2返回为1,2都除以102,未定义除以0 但是,在这个代码中,数字中包含0的数字(例如1001020)得到错误。 我得到“程序错误:前奏。读取:无解析” 任何帮助都将不胜感激。非常感谢 import Control.Monad import Data.Array import Data.Bits

Haskell代码问题 描述:代码应该返回数字中的多少位数除以整个数字。 例如,12有两个数字[1,2],这两个数字都是除2(12%2和12%1都是0),因此返回2,因为有两个数字将数字除。 对于102,2返回为1,2都除以102,未定义除以0

但是,在这个代码中,数字中包含0的数字(例如1001020)得到错误。 我得到“程序错误:前奏。读取:无解析”

任何帮助都将不胜感激。非常感谢

import Control.Monad
import Data.Array
import Data.Bits
import Data.Char
import Data.List
import Data.Set
import Debug.Trace
import System.Environment
import System.IO
import System.IO.Unsafe

findDigits :: Int -> Int
findDigits n = digits n n 0 (lengths n)
    where
        digits n on count endCheck
            | endCheck == 0 = count
            | header n == 0 = digits (tailer n) on count (endCheck-1)
            | on `mod` header n == 0 = digits (tailer n) on (count+1) (endCheck-1)
            | otherwise = digits (tailer n) on count (endCheck-1)

header :: Int -> Int
header x = digitToInt . head . show $ x

tailer :: Int -> Int
tailer x = read . tail . show $ x

lengths :: Int -> Int
lengths x = length . show $ x

我认为你在函数中做的太多了。实际上,最好使用每个函数都能解决一个简单任务的小函数,然后将这些函数组合成同样小的函数,并执行(稍微)更复杂的任务

例如,我们可以创建一个返回数字列表的函数
digits::Int->[Int]

digits :: Int -> [Int]
digits x | x >= 10 = r : digits q
         | otherwise = [x]
    where (q,r) = quotRem x 10
例如:

Prelude> digits 102
[2,0,1]
然后,我们可以过滤这些数字,以检查数字是否不为零(因为不可分割),以及数字是否可被该数字分割:

dividableDigits :: Int -> [Int]
dividableDigits n = filter (\x -> x /= 0 && mod n x == 0) (digits n)

现在的问题是计算匹配的数字。我把它留作练习。

我想你在函数中做的太多了。实际上,最好使用每个函数都能解决一个简单任务的小函数,然后将这些函数组合成同样小的函数,并执行(稍微)更复杂的任务

例如,我们可以创建一个返回数字列表的函数
digits::Int->[Int]

digits :: Int -> [Int]
digits x | x >= 10 = r : digits q
         | otherwise = [x]
    where (q,r) = quotRem x 10
例如:

Prelude> digits 102
[2,0,1]
然后,我们可以过滤这些数字,以检查数字是否不为零(因为不可分割),以及数字是否可被该数字分割:

dividableDigits :: Int -> [Int]
dividableDigits n = filter (\x -> x /= 0 && mod n x == 0) (digits n)

现在的问题是计算匹配的数字。我把它留作练习。

提示:
tailer204=4
但是
length 204-1/=length 4
。提示:
tailer204=4
但是
length 204-1/=length 4