Skip to content

Instantly share code, notes, and snippets.

@wzhd
Last active November 4, 2017 08:40
Show Gist options
  • Save wzhd/0f16475be5c975341e45f5d6c859ee02 to your computer and use it in GitHub Desktop.
Save wzhd/0f16475be5c975341e45f5d6c859ee02 to your computer and use it in GitHub Desktop.
console-wave
dist
cabal-dev
*.o
*.hi
*.chi
*.chs.h
.virtualenv
.hsenv
.cabal-sandbox/
cabal.sandbox.config
cabal.config

Print a sine wave to the terminal.

import Control.Concurrent (threadDelay)
putStrs :: [String] -> IO ()
putStrs (l:ls) = do
putStrLn l
threadDelay 50000
putStrs ls
plotLines :: [Int] -> [String]
plotLines (x0:x1:xs) = line : plotLines xs
where
line = map charForPos [0 .. max x0 x1]
charForPos :: Int -> Char
charForPos pos
| and $ map (== pos) [x0, x1] = '█'
| x0 == pos = '▀'
| x1 == pos = '▄'
| otherwise = ' '
f :: Float -> Int
f x = round $ 40 + 40 * sin(x / 9)
main :: IO()
main = putStrs $ plotLines $ map f [1..]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment