项目欧拉8 - 我不明白

时间:2015-03-28 01:08:02

标签: haskell

我在Haskell中寻找第8个Euler问题的解决方案,但我不太了解它。

import Data.List
import Data.Char

euler_8 = do
   str <- readFile "number.txt"
   print . maximum . map product
         . foldr (zipWith (:)) (repeat [])
         . take 13 . tails . map (fromIntegral . digitToInt)
         . concat . lines $ str

Here是解决方案的链接,here您可以找到该任务。

有人可以逐一解释我的解决方案吗?

3 个答案:

答案 0 :(得分:10)

读取数据

readFile读取文件"number.txt"。如果我们在名为number.txt

的文件中放入一个小的16位数字
7316
9698
8586
1254

乳宁

euler_8 = do
   str <- readFile "number.txt"
   print $ str

结果

"7316\n9698\n8586\n1254"

此字符串中包含额外的换行符。要删除它们,作者会将字符串拆分为lines

euler_8 = do
   str <- readFile "number.txt"
   print . lines $ str

结果不再包含任何'\n'个字符,而是一个字符串列表。

["7316","9698","8586","1254"]

要将其转换为单个字符串,字符串将concat一起使用。

euler_8 = do
   str <- readFile "number.txt"
   print . concat . lines $ str

连接字符串是字符列表而不是数字列表

"7316969885861254"

每个角色按Int转换为digitToInt,然后由Integer转换为fromInteger。在使用全尺寸Integer的32位硬件上非常重要,因为13位数的乘积可能大于2^31-1。此转化map位于列表中的每个项目上。

euler_8 = do
   str <- readFile "number.txt"
   print . map (fromIntegral . digitToInt)
         . concat . lines $ str

结果列表中充满了Integer s。

[7,3,1,6,9,6,9,8,8,5,8,6,1,2,5,4]

子序列

作者的下一个目标是在此整数列表中查找所有13位数的运行。 tails返回列表中的所有子列表,从任何位置开始并一直运行到列表末尾。

euler_8 = do
   str <- readFile "number.txt"
   print . tails
         . map (fromIntegral . digitToInt)
         . concat . lines $ str

这导致了我们的16位数示例的17个列表。 (我已经添加了格式化)

[
  [7,3,1,6,9,6,9,8,8,5,8,6,1,2,5,4],
    [3,1,6,9,6,9,8,8,5,8,6,1,2,5,4],
      [1,6,9,6,9,8,8,5,8,6,1,2,5,4],
        [6,9,6,9,8,8,5,8,6,1,2,5,4],
          [9,6,9,8,8,5,8,6,1,2,5,4],
            [6,9,8,8,5,8,6,1,2,5,4],
              [9,8,8,5,8,6,1,2,5,4],
                [8,8,5,8,6,1,2,5,4],
                  [8,5,8,6,1,2,5,4],
                    [5,8,6,1,2,5,4],
                      [8,6,1,2,5,4],
                        [6,1,2,5,4],
                          [1,2,5,4],
                            [2,5,4],
                              [5,4],
                                [4],
                                 []
]

作者将在我们重新排列这些列表的过程中提取一个技巧,以读取13位长的子列表。如果我们查看左对齐而不是右对齐的这些列表,我们可以看到每个列下的子序列。

[
  [7,3,1,6,9,6,9,8,8,5,8,6,1,2,5,4],
  [3,1,6,9,6,9,8,8,5,8,6,1,2,5,4],
  [1,6,9,6,9,8,8,5,8,6,1,2,5,4],
  [6,9,6,9,8,8,5,8,6,1,2,5,4],
  [9,6,9,8,8,5,8,6,1,2,5,4],
  [6,9,8,8,5,8,6,1,2,5,4],
  [9,8,8,5,8,6,1,2,5,4],
  [8,8,5,8,6,1,2,5,4],
  [8,5,8,6,1,2,5,4],
  [5,8,6,1,2,5,4],
  [8,6,1,2,5,4],
  [6,1,2,5,4],
  [1,2,5,4],
  [2,5,4],
  [5,4],
  [4],
  []
]

我们只希望这些列的长度为13位,因此我们只想take13行。

[
  [7,3,1,6,9,6,9,8,8,5,8,6,1,2,5,4],
  [3,1,6,9,6,9,8,8,5,8,6,1,2,5,4],
  [1,6,9,6,9,8,8,5,8,6,1,2,5,4],
  [6,9,6,9,8,8,5,8,6,1,2,5,4],
  [9,6,9,8,8,5,8,6,1,2,5,4],
  [6,9,8,8,5,8,6,1,2,5,4],
  [9,8,8,5,8,6,1,2,5,4],
  [8,8,5,8,6,1,2,5,4],
  [8,5,8,6,1,2,5,4],
  [5,8,6,1,2,5,4],
  [8,6,1,2,5,4],
  [6,1,2,5,4],
  [1,2,5,4]
]

foldr (zipWith (:)) (repeat [])转置列表列表(解释它可能属于another question)。它会丢弃比最短行长的行的部分。

euler_8 = do
   str <- readFile "number.txt"
   print . foldr (zipWith (:)) (repeat [])
         . take 13 . tails
         . map (fromIntegral . digitToInt)
         . concat . lines $ str

我们现在正常阅读列表中的子序列

[
  [7,3,1,6,9,6,9,8,8,5,8,6,1],
  [3,1,6,9,6,9,8,8,5,8,6,1,2],
  [1,6,9,6,9,8,8,5,8,6,1,2,5],
  [6,9,6,9,8,8,5,8,6,1,2,5,4]
]

问题

我们通过product ping map找到每个子序列的product

euler_8 = do
   str <- readFile "number.txt"
   print . map product
         . foldr (zipWith (:)) (repeat [])
         . take 13 . tails
         . map (fromIntegral . digitToInt)
         . concat . lines $ str

这会将列表减少为单个数字

[940584960,268738560,447897600,1791590400]

我们必须从中找到maximum

euler_8 = do
   str <- readFile "number.txt"
   print . maximum . map product
         . foldr (zipWith (:)) (repeat [])
         . take 13 . tails
         . map (fromIntegral . digitToInt)
         . concat . lines $ str

答案是

  

1791590400

答案 1 :(得分:4)

如果您不熟悉所使用的功能,首先应该检查每个功能的类型。由于这是函数组合,因此从内到外应用(即操作在读取时从右到左,从下到上)。我们可以逐行浏览。

从最后一行开始,我们将首先检查类型。

:t str
str :: String -- This is your input
:t lines
lines :: String -> [String] -- Turn a string into an array of strings splitting on new line
:t concat
concat :: [[a]] -> [a] -- Merge a list of lists into a single list (hint: type String = [Char])

由于type String = [Char](所以[String]等同于[[Char]]),此行将多行号转换为单个数字字符数组。更准确地说,它首先根据完整字符串创建一个字符串数组。也就是说,每个新行一个字符串。然后它将所有这些行(现在只包含数字字符)合并为一个字符数组(或单个String)。

下一行将此新String作为输入。再次,让我们观察类型:

:t digitToInt
digitToInt :: Char -> Int -- Convert a digit char to an int
:t fromIntegral
fromIntegral :: (Num b, Integral a) => a -> b -- Convert integral to num type
:t map
map :: (a -> b) -> [a] -> [b] -- Perform a function on each element of the array
:t tails
tails :: [a] -> [[a]] -- Returns all final segments of input (see: http://hackage.haskell.org/package/base-4.8.0.0/docs/Data-List.html#v:tails)
:t take
take :: Int -> [a] -> [a] -- Return the first n values of the list

如果我们将这些操作应用于我们的字符串当前输入,那么首先发生的是我们在字符串中的每个字符上映射(fromIntegral . digitToInt)的组合函数。这样做是将我们的数字串转换为数字类型列表。 编辑正如下面评论中所指出的,此示例中的fromIntegral是为了防止32位整数类型溢出。现在我们已经将字符串转换为实际的数字类型,我们首先在此结果上运行tails。由于(通过问题陈述)所有值必须是相邻的,并且我们知道所有整数都是非负的(由于是更大数字的位置),我们只采用前13个元素,因为我们要确保我们的乘法是13个连续元素的分组。如果不考虑下一行,这很难理解。

所以,让我们做一个快速的实验。将我们的字符串转换为数字类型后,我们现在有一个很大的列表列表。这实际上很难想到我们实际拥有的东西。为了便于理解,列表的内容不是很重要。重要的是它的大小。让我们来看一个人为的例子:

(map length . take 13 . tails) [1..1000]
[1000,999,998,997,996,995,994,993,992,991,990,989,988]

你可以看到我们这里有13个元素的大清单。每个元素是大小为1000的列表(即完整数据集),降序为988。所以这就是我们目前用于输入下一行的东西,可以说,这是最难理解但最重要的一行。当我们走完下一行时,为什么理解这一点很重要。

:t foldr
foldr :: (a -> b -> b) -> b -> [a] -> b -- Combine values into a single value
:t zipWith
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] -- Generalization of zip
:t (:)
(:) :: a -> [a] -> [a] -- Cons operator. Add element to list
:t repeat
repeat :: a -> [a] -- Infinite list containing specified value

还记得我之前提到过我们之前有13个元素的列表(不同大小的列表)吗?这很重要。该行将迭代该列表并将(zipWith (:))应用于该列表。 (repeat [])是这样的,每次在子序列上调用zipWith时,它都以空列表为基础开始。这允许我们构建一个列表列表,其中包含长度为13的相邻子序列。

最后,我们到达最后一行很容易。也就是说,我们仍然应该注意我们的类型

:t product
product :: Num a => [a] -> a -- Multiply all elements of a list together and return result
:t maximum
maximum :: Ord a => [a] -> a -- Return maximum element in the list

我们要做的第一件事是在每个子序列上映射product函数。完成后我们最终会得到一个数字类型列表(嘿,我们终于没有列表了!)。这些值是每个子序列的乘积。最后,我们应用maximum函数,它只返回列表中最大的元素。

答案 2 :(得分:0)

编辑:我后来发现了foldr表达式的用途。 (见下面的评论我的回答)。

我认为这可以用不同的方式表达 - 你可以在列表的末尾添加一个警卫。

该解决方案的详细版本将是:

import Data.List
import Data.Char

euler_8 = do
    let len  = 13
    let str1 = "123456789\n123456789"
    -- Join lines 
    let str2 = concat (lines str1)
    -- Transform the list of characters into a list of numbers
    let lst1 = map (fromIntegral . digitToInt) str2
    -- EDIT: Add a guard at the end of list
    let lst2 = lst1 ++ [-1]
    -- Get all tails of the list of digits
    let lst3 = tails lst2
    -- Get first 13 digits from each tail
    let lst4 = map (take len) lst3
    -- Get a list of products
    let prod = map product lst4
    -- Find max product
    let m    = maximum prod
    print m