Skip to content

Instantly share code, notes, and snippets.

@nfunato
Last active May 13, 2018 04:40
Show Gist options
  • Select an option

  • Save nfunato/6578105 to your computer and use it in GitHub Desktop.

Select an option

Save nfunato/6578105 to your computer and use it in GitHub Desktop.
-- AA graph drawer (as an exercise of mapAccum)
-- 2013-09-16 @nfunato
import Data.List (mapAccumL, transpose)
main = plot =<< getContents
plot = mapM_ putStrLn . convert
convert = transpose . makeVstrs . analyze . parse
parse = map cconv . filter (`elem` "RCF")
where cconv 'R' = (-1, '/' )
cconv 'C' = ( 0, '_' )
cconv 'F' = ( 1, '\\')
analyze = mapAccumL f (0,0,0)
where f (y,ymin,ymax) (d,c) = ((y+d, min y ymin, max y ymax), (y',c))
where y' = if c=='\\' then y+1 else y
makeVstrs ((_,ymin,ymax),yplots) = map makeVstr yplots
where makeVstr (y',c) = map (\y -> if y==y' then c else ' ') [ymin..ymax]
test1 = "RCRFCRFFCCRFFRRCRRCCFRFRFF"
{-
*Main> plot test1
__
/ \/\/\
_/\_/\ _/ \
/ \__/\ /
\/
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment