A nice way to answer this question is not to: instead write a program which will answer it for you: you want a program which, given some object, will return a description of a program which will make it for you. Here is one such:
(define (make-cons-tree-maker tree)
;; Note this assumes that the trees are trees.
(cond
[(null? tree)
;; If the tree is null then it is made by '(). This case is not needed
;; (why?)
'(quote ())]
[(cons? tree)
;; If the tree is a cons we make it by the cons function
`(cons ,(make-cons-tree-maker (car tree))
,(make-cons-tree-maker (cdr tree)))]
[(symbol? tree)
;; Symbols need quoting. This case is not strictly needed either
`(quote ,tree)]
[(or (number? tree)
(string? tree))
;; Numbers and strings do not need quoting
tree]
[else
;; But assume anything else does (this is safe)
`(quote ,tree)]))
And now
> (make-cons-tree-maker '(x y))
'(cons 'x (cons 'y '()))
In fact, as the comments in the function say, it can be made a lot simpler, if you are willing to accept that the instructions it returns are not minimal:
(define (make-cons-tree-maker tree)
;; Note this assumes that the trees are trees.
(if (cons? tree)
;; If the tree is a cons we make it by the cons function
`(cons ,(make-cons-tree-maker (car tree))
,(make-cons-tree-maker (cdr tree)))
;; otherwise just quote it
`(quote ,tree)))
This will return the same result for the above tree:
> (make-cons-tree-maker '(x y))
'(cons 'x (cons 'y '()))
But its result has a lot of unneeded quotes for some other trees:
> (make-cons-tree-maker '(1 2 3 "foo" . 4))
'(cons '1 (cons '2 (cons '3 (cons '"foo" '4))))
And finally you can of course do this, which entertains you if this sort of thing entertains you:
> (make-cons-tree-maker '(define (make-cons-tree-maker tree)
;; Note this assumes that the trees are trees.
(if (cons? tree)
;; If the tree is a cons we make it by the cons function
`(cons ,(make-cons-tree-maker (car tree))
,(make-cons-tree-maker (cdr tree)))
;; otherwise just quote it
`(quote ,tree))))
'(cons
'define
(cons
(cons 'make-cons-tree-maker (cons 'tree '()))
(cons
(cons
'if
(cons
(cons 'cons? (cons 'tree '()))
(cons
(cons
'quasiquote
(cons
(cons
'cons
(cons
(cons
'unquote
(cons
(cons
'make-cons-tree-maker
(cons (cons 'car (cons 'tree '())) '()))
'()))
(cons
(cons
'unquote
(cons
(cons
'make-cons-tree-maker
(cons (cons 'cdr (cons 'tree '())) '()))
'()))
'())))
'()))
(cons
(cons
'quasiquote
(cons (cons 'quote (cons (cons 'unquote (cons 'tree '())) '())) '()))
'()))))
'())))