2
\$\begingroup\$

You are to create a program, that, given another program, and a position, will make it a quine. Namely, it needs to insert an expression at that point that evaluates to a string that is the programs input. For example:

main=do
    putStrLn "Welcome to the open source program, would you like my source code?"
    (ans:_) <- getLine
    case ans of
        'y' -> putStrLn  --Quine goes here
        'n' -> putStrLn "Okay, Good bye"
        _  -> putStrLn "Unrecongnized command. Try again." >> main

Now if I told it to put the source code at (5,24) the result would be.

main=do
    putStrLn "Welcome to the open source program, would you like my source code?"
    (ans:_) <- getLine
    case ans of
        'y' -> putStrLn ((\a b->a++(show a)++" "++(show b)++b) "main=do\n    putStrLn \"Welcome to the open source program, would you like my source code?\"\n    (ans:_) <- getLine\n    case ans of\n        'y' -> putStrLn ((\\a b->a++(show a)++\" \"++(show b)++b) " ")--Quine goes here\n        'n' -> putStrLn \"Okay, Good bye\"\n        _  -> putStrLn \"Unrecongnized command. Try again.\" >> main\n")--Quine goes here
        'n' -> putStrLn "Okay, Good bye"
        _  -> putStrLn "Unrecongnized command. Try again." >> main

Clarifications:

  • The program you make and the type of programs it act upon may be different if you like.
  • Your program may get the position in the program and its source code however it sees fit. You may put the new source code where ever you see fit.
  • Obviously, the new program can't simply grab its source code from its file
  • You may only insert things at the position the user specifies (so you may not be able to include any new modules in the new program depending on target language)

This is code golf, so shortest answer wins! Let us see how many different languages we can make quine makers for as well.

\$\endgroup\$
3
\$\begingroup\$

J - 73 char

Takes input on stdin, as the 0-origin index in characters and the source, separated by a newline. Introduces a copy of the program at the specified point as a string.

((".@{.~({.,}.,~'(',')',~(LF;''',LF,''')rplc~quote@])(}.~>:))i.&LF)1!:1]3

Explained by explosion:

(                                  )1!:1]3  NB. stdin
 (                           )i.&LF         NB. find the first newline
     {.~                                    NB. the index part of input
  ".@                                       NB. convert index to integer
                      (}.~>:)               NB. the source program part
        ({.,}.,~     )                      NB. program before and after index
                ...@]                       NB. program quoted properly

For example, given input

3
abcdef'ghij'
kl mn 0p1

the output is

abc('abcdef''ghij''',LF,'kl mn 0p1')def'ghij'
kl mn 0p1

because J uses Pascal-style strings: 'Isn''t this great?'

\$\endgroup\$
1
\$\begingroup\$

Haskell (390 Char)

import System.Environment
import Data.List
main = do
    [p] <- getArgs
    interact $ q (read p)
q :: (Int, Int) -> String -> String
q p j=case d p j of
    (f, s) -> b (f++i) (')':s)
d (l,c) j=case splitAt (l-1) (lines j) of
    (f, (t:s)) -> case splitAt c t of
        (r,y) -> ((unlines f)++r, y++"\n"++(unlines s))
b=(\a b->a++(show a)++" "++(show b)++b)
i="((\\a b->a++(show a)++\" \"++(show b)++b) "

Not very golfed. Just an example. This takes the position as a command line argument and source code as a standard input and puts the new source code as standard output.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.