[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [MIT-Scheme-users] how to search a string in lists
From: |
Broseph |
Subject: |
Re: [MIT-Scheme-users] how to search a string in lists |
Date: |
Tue, 12 Jun 2012 20:18:41 -0700 (PDT) |
santonoreg wrote:
>
> Hi,
>
> I've the following code..
>
> (define FullList '(
> (define f1 '((name (George))
> (address (Kalavrytwn))
> (phone 2103333333)))
> (define f2 '((name (Spyros))
> (address (Stadiou))
> (phone 2102222222)))
>
>
> Question: How i can make a function to take as parameters the list, phone
> and search if the phone number exists. Also i want to display the name of
> the phone number owner.
>
> Thanks in advance,
>
> Sorry for my bad english!!!!
>
Well, if your list is like this:
(((George) (Kalavrytwn) (2103333333))
((Spyros) (Stadiou) (2102222222)))
Then you can write something like this:
(define (existing-number? address-book phone-number)
(if (null? address-book)
#f
(let* ((first-contact (car address-book))
(first-phone (caddr first-contact))
(rest-of-contacts (cdr address-book)))
(if (equal? first-phone phone-number)
#t
(existing-number? rest-of-contacts phone-number)))))
There is probably a prettier way of doing this, and if your list is
presorted with respect to the phone numbers, then you could do a binary
search instead of this linear one.
--
View this message in context:
http://old.nabble.com/how-to-search-a-string-in-lists-tp33997816p34003480.html
Sent from the Gnu - MIT Scheme - Users mailing list archive at Nabble.com.