0

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)}
Will Ness
  • 70,110
  • 9
  • 98
  • 181
J. Doe
  • 95
  • 8
  • What is the code you are having trouble with? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Please, provide a [mcve]. – Jörg W Mittag Mar 24 '18 at 10:35
  • "Scheme doesn't let me do it" is not a precise enough error description for us to help you. *What* doesn't work? *How* doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? – Jörg W Mittag Mar 24 '18 at 10:35
  • Can you provide a *precise* specification of what it is that you want to happen, including any and all rules, exceptions from those rules, corner cases, special cases, boundary cases, and edge cases? Can you provide sample inputs and outputs demonstrating what you expect to happen, both in normal cases, and in all the exceptions, corner cases, special cases, boundary cases, and edge cases? Please, also make sure to provide a [mcve]. – Jörg W Mittag Mar 24 '18 at 10:35
  • Please, post your code and error messages. Your code, not photographs of your code. This is a programming site, not a photography site. We want to copy&paste&debug your code, not critique its color and composition. You are expecting everybody who wants to help you to try and decipher the code from a small picture, then type it in. – Jörg W Mittag Mar 24 '18 at 11:03
  • I put a screenshot of the program (in the last line in the question). The result I want to have in mathematics is: If n=0 return ϕ, else, return ω(n-1)∪{ω(n-1)}. In scheme it's translated to: If n=0 return ϕ, else, return a union list of (omega (- n 1)) with a list that contains a single variable which is (omega (- n 1)). – J. Doe Mar 24 '18 at 11:06
  • I'll copy my code. – J. Doe Mar 24 '18 at 11:06
  • What is the purpose of the `quote` symbols in the result? – Sylwester Mar 24 '18 at 20:57

1 Answers1

3

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.

Will Ness
  • 70,110
  • 9
  • 98
  • 181