質問

I've got a list defined like this:

(define testlist '((Dog <=> Cat)
                   (Anne <=> Dodd))

Is there any way to turn: (car testlist) into a string so I can use regexp on it to search for "<=>"?

役に立ちましたか?

解決

Let me start with this extremely relevant Jamie Zawinski quote:

Some people, when confronted with a problem, think, “I know, I'll use regular expressions.” Now they have two problems.

You really really don't want to use regular expressions here. For one thing, a regexp-based solution will break when you have identifiers with <=> in the middle of them.

For another, it's really easy to solve this problem without using regular expressions.

There are a whole bunch of "right answers" here, depending on what exactly you're trying to do, but let me start by pointing out that you can use the "member" function to see whether a list contains the symbol '<=> :

#lang racket

(define testlist '((Dog <=> Cat)
                   (Anne <=> Dodd)))

(cond [(member '<=> (car testlist)) "yep"]
      [else "nope"])

I suspect that you're trying to parse these as logical equivalences, in which case you'll need to define the possible structures of the statements, and go from there, but let's just start by NOT USING REGULAR EXPRESSIONS :).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top