Haskell:在水平线上移动图像 [英] Haskell: moving image on horizontal line

查看:259
本文介绍了Haskell:在水平线上移动图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Im对于Haskell来说是新的,并且正在使用表示为类型Img = [String]
我想用一个换行向左或向右移动一行或多行图像。



我已经管理图像向上或向下移动,代码如下:

  moveVer :: Int  - > Img  - > Img 
moveVer n xs = take len $ drop(mod n len)$ cycle xs
where len = length xs

img 1 = XXXXXX OUTPUT =(moveVer(3)( img 1))= XX
XX XX
XX XXXXXX
XX XXXXXX
XXXXXX XX

现在我正在尝试做同样的事情,但水平移动图像(向左或向右)。
函数定义 moveHor :: Int - > Img - > Img

解决方案

基本算法与垂直移位函数相同,因为 String 也是一个字符列表, [Char]



你可以将算法概括为 move 函数,因为 moveVer 的代码实际上只需要一个列表并且不依赖于 Img

move :: Int - > [a] - > [a]
move n xs = take len $ drop(mod n len)$ cycle xs
where len = length xs

现在您可以用 move :来重写您的 moveVer / p>

moveVer :: Int - > Img - > Img
moveVer = move

由于字符串只是一个字符列表,所以可以映射到字符串列表并使用 move 函数来执行水平移动。

  moveHor :: Int  - > Img  - > Img 
moveHor n = map $ move n


Im new to Haskell and am working with images represented as type Img = [String]. I want to move the image either left or right by 1 or more rows with a wrap.

i've manged to move the images up or down, code below:

moveVer :: Int -> Img -> Img
moveVer n xs = take len $ drop (mod n len) $ cycle xs
      where len = length xs

img 1 = XXXXXX       OUTPUT = (moveVer (3)(img 1))  = XX
          XX                                          XX
          XX                                        XXXXXX  
          XX                                        XXXXXX
        XXXXXX                                        XX

Now i'm trying to do the same thing but move the image horizontally (left or right). function to defined moveHor :: Int -> Img -> Img

解决方案

The fundamental algorithm is the same as your vertical shift function, since String is also a list of characters, [Char].

You can generalize your algorithm to a move function since the code for moveVer really only requires a list and has no dependency on Img:

move :: Int -> [a] -> [a]
move n xs = take len $ drop (mod n len) $ cycle xs
  where len = length xs

Now you can rewrite your moveVer function in terms of move:

moveVer :: Int -> Img -> Img
moveVer = move

And since a string is just a List of characters, you can map over the list of strings and use the move function to do your horizontal movement.

moveHor :: Int -> Img -> Img
moveHor n = map $ move n

这篇关于Haskell:在水平线上移动图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆