Created
October 7, 2017 14:41
-
-
Save schmidsi/b0a027ab6b359a1e87bd99bd8173ebc0 to your computer and use it in GitHub Desktop.
Fun with Haskell Lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PictureASCII where | |
import Data.Char | |
-------------------------------------------------------------------------------- | |
-- 1) ASCII Pictures | |
-------------------------------------------------------------------------------- | |
-- A Picture is a list of strings. | |
type Picture = [String] | |
-- An example picture. Design a nicer one. | |
lambda :: Picture | |
lambda = [ | |
" ", | |
" #### ", | |
" ### ", | |
" ### ", | |
" #### ", | |
" ## ### ", | |
" ## ### ", | |
" ### ### ", | |
" "] | |
rabbit :: Picture | |
rabbit = [ | |
" (\\ /) ", | |
" .\\\\_//. ", | |
" )0 0( ", | |
" ( (_) ) ", | |
" `'\"'` "] | |
-- Run 'main' to see an example. | |
main :: IO () | |
main = printPicture lambda | |
-- Prints the given picture to stdout. | |
printPicture :: Picture -> IO () | |
printPicture p = putStr (unlines p) | |
-- a) Mirrors the given picture horizontally. | |
flipH :: Picture -> Picture | |
flipH p = reverse p | |
-- b) Mirrors the given picture vertically. | |
-- hint = "Wrqr Mrvyr zhff frcneng hztrxrueg jreqra." | |
flipV :: Picture -> Picture | |
flipV p = map reverse p | |
-- c) Takes two pictures and puts the first above the second. | |
-- hint = "Qnf fbyygra Fvr nhpu buar Uvysr uvaxevrtra." | |
above :: Picture -> Picture -> Picture | |
above p1 p2 = p1 ++ p2 | |
-- d) Takes two pictures and puts the first left of the second. | |
-- hint = "Irejraqra Fvr qvr Shaxgvba mvcJvgu hz wr mjrv Mrvyra mh xbaxngravrera." | |
beside :: Picture -> Picture -> Picture | |
beside p1 p2 = zipWith (++) p1 p2 | |
-------------------------------------------------------------------------------- | |
-- 2) Functions on Lists | |
-- hint = "\65533oreyrtra Fvr fvpu mhrefg qvr Fvtanghe" | |
-------------------------------------------------------------------------------- | |
-- a) Append an element at the end. | |
append :: a -> [a] -> [a] | |
append element list = list ++ [element] | |
-- b) Reverse all but the first and the last element. | |
reverseInner :: [a] -> [a] | |
reverseInner l = [head l] ++ reverse (tail (init l)) ++ [last l] | |
-- c) Insert an element at a given position. | |
insertAt :: a -> Int -> [a] -> [a] | |
insertAt el at list = (take at list) ++ [el] ++ (drop at list) | |
-- Functions to decode hints. | |
-- Usage: decodeHint "Mhrefg anpuqraxra, qnaa Gvccf nafpunhra =)" | |
decodeHint :: String -> String | |
decodeHint = map rot13 | |
where rot13 c | |
| toLower c >= 'a' && toLower c <= 'm' = chr (ord c + 13) | |
| toLower c >= 'n' && toLower c <= 'z' = chr (ord c - 13) | |
| otherwise = c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment