import System.IO;main=mapM(`hSetEncoding`utf8)[stdin,stdout]>>interact(f.span('$'/=));f(b,_:_:r)=lines r?[]where(x:y)?('@':_:z)=x++y?z;x?(y:z)=y:x?z;("1":y)?_=y?b;_?z=z
Note that non-ascii characters in the above source code will be escaped (such as \x9f).