-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPlotter.hs
More file actions
33 lines (21 loc) · 858 Bytes
/
Plotter.hs
File metadata and controls
33 lines (21 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module Plotter where
import Data.List(sort)
--plotter :: ( Int -> Int )-> String
makeReverseCoord :: (Int->Int)->[(Int,Int)]
makeReverseCoord f = [(f x, x) | x <- [1..20]]
findGreatestY :: [(Int, Int)] -> Int
findGreatestY xs = maximum [fst x | x <- xs]
removeNegative :: [(Int, Int)] -> [(Int, Int)]
removeNegative xs = [x | x <- xs, fst x >= 0]
generateLine :: Int -> String
generateLine x = replicate x ' ' ++ "*\n"
printGraph 0 _ = ""
printGraph l [] = concat (replicate l "\n")
printGraph l (x:xs)
| l == fst x = generateLine (snd x) ++ printGraph (l - 1) xs
| otherwise = "\n" ++ printGraph (l - 1) (x:xs)
plotter :: (Int -> Int) -> String
plotter f = let coord = makeReverseCoord f
posCoord = removeNegative coord
maxY = findGreatestY posCoord
in printGraph maxY (reverse (sort posCoord))