Haskell将点列表转换为字符串

Haskell将点列表转换为字符串

本文介绍了Haskell将点列表转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在完成这项任务,我需要一些帮助。这是最后一部分,但我真的很困难,不知道如何处理。这是问题:
$ b

I am working on the assignment and I need some help. This is the last part but I am really struggling with it, don't know how to approach. Here's the problem:

".|...\n.xxx.\n-+x--\n.|...\n"

(其中图像中的点表示为'x',原点表示为
'+',水平轴和垂直轴分别表示为' - '和'|'
):

(where the points in the image are denoted 'x', the origin is denoted '+', and the horizontal and vertical axes are denoted '-' and '|' respectively):

.|...
.xxx.
-+x--
.|...

通过渲染生成t函数可以通过执行putStr
(render t)来实现上述结果。)

(String we produce by render t function can be printed by doing putStr (render t) to achieve above result).

图像和t就是这样:

type Point = (Int,Int)
type Image = [Point]
t :: Image
t=[(0,1),(1,0),(1,1),(2,1)]


首先,我需要另一个函数,因为它不能在渲染中完成。我知道要查看y和x的最大/最小值来获得边界,并且我有一个函数。我还有一个函数,它会在每个n个字符后插入换行符。

To start off, I will need another function since it can't all be done in render. I know to look at max/min values of y and x to get boundaries, and I have a function for that. I also have a function which will insert newline into the string after every n characters.

推荐答案

所以你有一个函数让你尺寸:

So you have a function to get your dimensions:

dimensions :: Image -> (Point, Point)

返回左上角和右下角。然后,我会按排序顺序获得积分:

that returns the upper left and lower right corners. I would then get the points in sorted order:

sortImage :: Image -> Image

列出了最大的 y 和最小 x 第一(提示: Data.List.sortBy 是你的朋友)。这实际上并非必要,但它可以让事情稍后变得容易。然后,您可以创建一个空白图像(即只有坐标轴和'。',或者您可以使用空格来获得更清晰的外观)

that lists sorts by largest y and smallest x first (hint: Data.List.sortBy is your friend). This isn't actually necessary, but it can make things easier later. Then you can create a blank image (i.e. only axes and '.', or you can use spaces for a cleaner look)

blankImage :: (Point, Point) -> [String]

超出您的尺寸。一定要返回行的列表,你可以稍后用新行加入,但现在让它更容易处理。现在, [String] = [[Char]] ,所以你有一个2D字符数组。你有一个 Point s的列表来指示坐标,但是你必须改变它们,这样左上角的坐标现在是(0,0) 。这样我们就可以在数组上使用普通索引来设置我们的点。幸运的是,我们已经在维中计算了偏移值。

out of your dimensions. Be sure to return a list of the rows, you can join with new lines later, but for now keep it easier to work with. Now, [String] = [[Char]], so you have a 2D array of characters. You have a list of Points indicating coordinates, but you'll have to shift them so that your upper left coordinate is now (0, 0). This is so we can use normal indexing on the arrays to set our points. Luckily, we've already calculated the offset value in dimensions.

然后您需要一个函数 blankImage 的输出,并使用您现在偏移的 Image 中的值替换字符。由于 Point s现在是您的 blankImage 中的索引,所以这应该很简单。

Then you'll want a function that takes the output of blankImage and replaces characters using the values in your now offset Image. Since the Points are now indices in your blankImage, this should be pretty easy.

fillImage :: Image -> [String] -> [String]

所以这个过程是:

So the process is:

import Data.List

showImage :: Image -> String
showImage img = intercalate "\n" filled
    where
        sortedImg = sorteImage img
        (upperL, lowerR) = dimensions sortedImg
        blank = blankImage (upperL, lowerR)
        offsetImg = offsetImage upperL sortedImg
        filled = fillImage offsetImg blank
    putStrLn $ intercalate "\n" filled

函数 intercalate 会将您的 [String] 加入新行,把它变成一个大字符串。

The function intercalate will join your [String] with new lines, turning it into one big string.

我从你的评论中看到你是Haskell的新手,如果你需要更多的帮助,我会提供一些更多的提示,但是你最好先尝试自己解决这个问题。如果您遇到困难,请发表评论,我会编辑我的答案,以帮助您跨越障碍。

I see from your comments that you're new to Haskell, so I'll say that if you need more help, I'll give some more hints, but it'd be much better for you to try to work through this yourself first. If you get stuck, comment and I'll edit my answer to help you over the hurdle.

这篇关于Haskell将点列表转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:20