-4

Lets say that we got a list like this(with much more elements but the method should be the same):

(define l '((cons 'name 'john)
            (cons 'sur 'asurname)
            (cons 'name 'george)
            (cons 'sur 'georgesurname)))

and we can always add more elements to the existing list. Which is the most effective way lets say to write a function that takes the name of a the user and returns the surname:

something like :

(define display-surname
  (lamdba (name)
   (...)))

What is the general practice in such cases? Can anyone point an example/link of a how to?

Óscar López
  • 232,561
  • 37
  • 312
  • 386
spk
  • 153
  • 9
  • For those who have the time to put a minus but not to answer: Heres some additional info: The list is saved in a text file.There's a procedure that adds contacts.Every contact has (name,surname,phone,email). Opening and writing to file is not a problem. Since it's not a simple list i would like to point me some info/link not to show me the solution. The list can expand anytime so a simple (car l) for example is not what i seek. Sure it would be with cars and cdrs. But is there a technique for nested lists? – spk May 11 '12 at 22:21
  • I assume you mean something like `'((name . Lord) (sur . Piggles) (name . Captain) (sur . Hamster))`? Because your current example seems to misuse the quoting a little? – Lily Chung May 11 '12 at 22:46
  • @Arafinwe Yes correct, my bad, that's what i ment. – spk May 11 '12 at 22:55
  • 4
    I'm hitting -1 because this question is poorly spelled, is poorly punctuated, displays a low enough level of research that the poster has missed [association list](http://www.cse.iitb.ac.in/~as/mit-scheme/scheme_12.html#SEC115) documentation, and has added information in a snippy comment rather than an edit. Please fix at least two of these things. – Inaimathi May 12 '12 at 01:05
  • 1
    And please make sure to type the list as it was _really_ intended. I'm left wondering if your data looks like this: `'((name . a) (sur . b) (name . c) (sur . d))` (not a very useful representation) or like this: `'(((name . a) (sur . b)) ((name . c) (sur . d)))` (makes much more sense) – Óscar López May 12 '12 at 01:17

1 Answers1

2

It'd be a better idea to represent each record (name,surname) in a single list, and then handle your data as a list of lists of pairs:

(define lst '(((name . a) (sur . b)) ((name . c) (sur . d))))

With the above representation, finding a name (given a surname) is as simple as this (assuming that each field in the record is always found in the same position):

(define display-surname
  (lambda (lst name)
   (cond ((null? lst) #f)
         ((eq? (cdaar lst) name) (cdadar lst))
         (else (display-surname (cdr lst) name)))))

You can simplify things even further (again assuming that each field in the record is always in the same position) by representing the above data as follows, with the corresponding changes in display-surname:

(define lst '((a . b) (c . d)))

(define display-surname
  (lambda (lst name)
   (cond ((null? lst) #f)
         ((eq? (caar lst) name) (cdar lst))
         (else (display-surname (cdr lst) name)))))

Either way, this works:

(display-surname lst 'a)
> 'b

(display-surname lst 'x)
> #f
Óscar López
  • 232,561
  • 37
  • 312
  • 386