LISP clause for and clause let ¿why?,making a programming language in racket using ragg

115 Views Asked by At

I have long been trying to find the error, I'm doing a programming language and have the next code, using ragg, I have a syntax-object(resto ...) what has a bracket as data, I transform this syntax-object to a datum:

   (let ([i (syntax->datum #'(resto ...))])
           (display "Content i:")
            (display i)
           (if (eq? i (string->symbol "(})"))
               (display "true")
               (display "false")
            )
   )

and the output is:

Content: (})
false

But if I do this

   (for ([i (syntax->datum #'(resto ...))])
     (displayln "Content:")
     (displayln i)
     (if (eq? i (string->symbol "}"))
           (display "true")
           (display "false")
     )
   )

and the output is:

Content: }
true

MY QUESTION: ¿WHY THE IF OF CLAUSE LET IS FALSE? ¿AS I CAN COMPARE THESE TWO TYPES AND THAT THE RESULT IS TRUE WITHOUT THE FOR?

Documentation about functions:

syntax->datum

1

There are 1 best solutions below

0
On BEST ANSWER

Each piece of code is doing a very different thing, I'll show you how to make each one work. The first one uses let to assign into a variable the whole list returned by syntax->datum, and afterwards you compare it against another list (better use equal? for testing equality, it's more general):

(let ([i (syntax->datum #'(|}|))]) ; `i` contains `(})`
  (display "Content i: ")
  (displayln i)
  (if (equal? i '(|}|)) ; a list with a single element, the symbol `}`
      (displayln "true")
      (displayln "false")))

The second piece of code is using for to iterate over each element in a list, until it finds the symbol }:

(for ([i (syntax->datum #'(|}|))]) ; `i` contains `}`
  (display "Content i: ")
  (displayln i)
  (if (equal? i '|}|) ; the symbol `}`
      (displayln "true")
      (displayln "false")))

As a side note, you have to be very careful with the way you're going to process all those curly brackets {} in your language, they're interpreted as normal parentheses () in Racket and they'll be tricky to handle, notice how I had to escape them by surrounding them with vertical bars.