1

I'm trying to figure out a question that takes a list of first names and a list of last name and create a new list of email addresses with the first letter of the first name and up to 7 letters of the last name and @yahoo.com.

example:

(check-expect 
    (emails-list (list "John" "Sarah") (list "King" "Dickinson")) 
    (list "jking@yahoo.com" "sdickins@yahoo.com"))

(check-expect (emails-list empty empty) empty)

So far I have:

(define (appendnames alof alos)
  (cond [(and (empty? alof) (empty? alos)) empty]
        [else (string-append 
                (substring (first alof) 0 1) 
                (cond [(< (string-length (first alos)) 8) (first alos)]
                      [else (substring (first alos) 0 7)]) 
                "@yahoo.com")]))

(define (emails-list alof alos)
  (cond [(and (empty? alof) (empty? alos)) empty]
        [else (appendnames alof alos)]))

What I don't know how to do is how to make the first letters lowercase and where to put in the recursion so that appendnames will (appendnames (rest alof) (rest alos)).

Thanks so much for any help I can get!

4

2 回答 2

1

Well, this looks like homework so I'll give you some hints, it'll be much better if you try to solve the problem by your own means. The first thing is - split the problem in two parts to make it simpler:

(define (emails-list alof alos)
  (if <???>                              ; if any of the lists is empty
      <???>                              ; return the empty list
      (cons (make-email <???> <???>)     ; create a new email with current values
            (emails-list <???> <???>)))) ; and advance the recursion

The interesting part of course, will be creating the actual email. Refer to the character and string procedures available, here's the general idea:

(define (make-email name surname)
  (<???>                    ; convert the whole string to lowercase
   (<???>                   ; append the three parts of the email
    (string (<???> name 0)) ; create a new string with the frist char in name
    <???>                   ; create a substring with last name (*)
    "@yahoo.com")))         ; add the email domain

The step marked with (*) requires a bit more explanation. Notice that we're interested in at most seven characters from the surname, which can be easily obtained with the substring procedure, as long as we remember that the end index is either 7 or the string's length, if the string's length is less than seven.

于 2013-03-18T22:19:10.623 回答
0

If you haven't already read it, you should read this chapter of HtDP, which explains how to design functions that take two complex inputs:

http://htdp.org/2003-09-26/Book/curriculum-Z-H-22.html

The HtDP chapter talks about three kinds of situations (or "cases") in which you need to process two complex arguments. Which situation matches the problem you're trying to solve?

How many cond clauses should you have? What are they? What do you have available to work with in the answer part of each clause? (In other words, what is the function's template?)

Once you get that far, it should be pretty easy to fill in the code. If you get stuck, use your concrete examples to help you.

于 2013-03-18T22:30:57.530 回答