recursionschemeracketset-union

list of a list in scheme


I have a list that I get from a lambda, and I want to create (and return) a list that contains an element of a list. Scheme doesn't let me do it.

#lang racket
(define omega
  (lambda (n)
    (if (= n 0) 'Φ
        (cons (omega (- n 1)) '(omega (- n 1))))))

Here're 2 outputs:

>  (omega 0)
'Φ
> (omega 1)
'(Φ omega (- n 1))
> (omega 2)
'((Φ omega (- n 1)) omega (- n 1))

The first output is correct, but I want that the second output will be:

'(Φ '(Φ))

and that (omega 2) will return

'(Φ '(Φ) '(Φ '(Φ)))

Actually, the result that I want to have, in mathematical notation, is:

ω(n)  =  If n=0 return ϕ, else, return ω(n-1)∪{ω(n-1)}

Solution

  • To get your desired output, start by changing it to

    (define omega
      (lambda (n)
        (if (= n 0) 'Φ
            (cons (omega (- n 1)) 
              ;; '(omega (- n 1))
                  (list 'quote (omega (- n 1)))
                  ))))
    

    which isn't producing exactly what you wanted, but the main thing is, you want to include the result of evaluating your nested code, not the code itself like you were doing.


    In the comments you write that what you actually want is

    ω n  =  IF  n=0  THEN  ϕ  ELSE  ω(n-1)∪{ω(n-1)}
    

    which actually translates as

    (define omega
      (lambda (n)
        (if (= n 0) (list 'Φ)        ; must enclose it in a list to avoid errors
            (append (omega (- n 1))
                    (list (omega (- n 1)))))))
    

    which produces

    '(Φ)
    '(Φ (Φ))
    '(Φ (Φ) (Φ (Φ)))

    in DrRacket.