views:

376

answers:

3

I want it to extract all the words that have a letter e in them. eg.

(ewords '(i e ee o oo)) -> '(e ee)

Berkeley's 61a lecture uses (first 'word) to extract the first character of the word. However DrScheme screams at me when I try to do that. How do take the first character of the word? like

(first 'word)->'w.
+2  A: 

You'll need to convert it to a string explicitly:

(first (symbol->string 'word)) -> "w"

(I'm not sure off the top of my head whether first operates on strings. You can use string->list to convert to a list and then take the first if it doesn't.)

EDIT: It might be easier to pass around strings instead of symbols in the first place -- to designate a string, use double quotes (e.g. (ewords '("i" "e" "ee" "o" "oo")).) I don't know if this is your intent, but since you're learning, I thought I should mention the distinction.

mquander
Hmm using strings sounds good but i really wanted to use symbols. thanks anyway.
kunjaan
Yes, the string->list is necessary.
Jay Kominek
+1  A: 

Symbols are primarily meant to be atomic values, i.e., they are not usually taken apart and examined. If you really want to do it you must first do some type conversions:

    ; #\a -> a
    (define (char->symbol chr)
      (string->symbol (list->string (list chr))))

    ; abc -> (a b c)
    (define (symbol->list sym)
      (map char->symbol 
           (string->list (symbol->string sym))))

You can now use first:

    > (first (symbol->list 'word))
    w
Jonas
A: 

It depends, what is the data definition for a "word"?

Is it a sequence of characters (aka a string)? Is it a list of characters? A list of strings? A list of symbols?

Here is a function that meets your criteria, but as you can see, it is convoluted and I wonder if it is what you really want:

; symbol->list-of-symbols : symbol -> (ListOf symbol)
; To destructure a symbol into a list of its constituent parts
(define (symbol->list-of-symbols sym)
  (map (lambda (s) (string->symbol (string s))) 
       (string->list (symbol->string sym))))
grettke