Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Escape characters and the intriguing effect of not escaping (
#1
I came across this phenomenon while editing strings. I walked down a string using car and cdr, attempting to remove all ('s, )'s, and .'s (I know that's an awful, hack thing to do, and I know I'm going down when I die). The motive here is to figure out why the open parenthesis works but the closed parenthesis (and the dot) doesn't. I'm assuming (as the title suggests) that this might have something to do with the need to escape these characters?

This works to delete all the "(" symbols in a cond statement:
((equal? (car s) "(") (f (cdr s) (+ i 1)))

A similar statement works to delete all the x characters.
((equal? (car s) "x") (f (cdr s) (+ i 1)))

These don't work to delete the ")" and "." symbols:
((equal? (car s) ")") (f (cdr s) (+ i 1)))
((equal? (car s) ".") (f (cdr s) (+ i 1)))

I'm really curious as to why one parenthesis works but the other doesn't. Anyone know anything about this?
"Watch as I topple an empire by changing a 1 to a 0." -Rick
Reply
#2
Not sure. I would try comparing against a symbol though, i.e. (equal? (car s) '.) for testing for a period, and see if that works as expected.

Also, if this is for task 4 in assign2, it might be helpful to approach your input as a list of symbols, rather than as a string.
UA ACM Vice President
ACM has bi-weekly meetings Tuesdays at 5:15pm
We're UA's best organization for CS majors (website)
Join us on Slack for all kinds of discussion channels (including one for CS403)
Reply
#3
Seems to work for me:

scam> (define s ")(.")
)(.
scam> s
)(.
scam> (equal? (car s) ")")
#t
scam> (equal? (car s) "(")
#f
scam> (equal? (cadr s) "(")
#t
scam> (equal? (cadr s) ")")
#f
scam> (equal? (caddr s) ".")
#t
scam> (equal? (caddr s) "+")
#f

You must use equal?, even for one letter strings.
Reply
#4
This is my solution for your task. It appears to work as you desire:

(define (remove char s)
   (cond
       ((null? s) nil)
       ((equal? char (car s)) (remove char (cdr s)))
       (else (string-append (car s) (remove char (cdr s))))
       )
   )
Reply
#5
Thanks much  davidmmcoy and Dr. Lusth! The above code works for me!

Just out of curiosity, do you see anything wrong with the function (pretty2 s)? (pretty s) calls the remove function from above which works perfectly, but (pretty2 s) tries to remove those characters itself which doesn't work. It's funny because as far as I can tell they seem to be identical except (pretty s) calls the remove function whereas (pretty2 s) removes the chars itself.


(define (remove char s)
    (cond
        ((null? s) nil)
        ((equal? char (car s)) (remove char (cdr s)))
        (else (string-append (car s) (remove char (cdr s))))
        )
    )
(define (pretty s)
    (remove ")" (remove "(" s))
    )
(define (pretty2 s)
    (cond
        ((null? s) nil)
        ((equal? (car s) "(") (pretty (cdr s)))
        ((equal? (car s) ")") (pretty (cdr s)))
        (else (string-append (car s) (pretty2 (cdr s))))
        )
    )

Results of (pretty s):
Exactly as expected

Result of (pretty2 s):
garbage collection and infinite recursion
"Watch as I topple an empire by changing a 1 to a 0." -Rick
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)