[Haskell-cafe] Re: Silly question: accessing a slot in a data type

Achim Schneider barsoap at web.de
Wed Jan 9 10:39:14 EST 2008


Fernando Rodriguez <frr149 at easyjob.net> wrote:

> 
> Hi,
> 
> I'm writing a very simple address book. I defined the follwoing types
> for Contact and AddressBook:
> 
> type Name= String
> type PhoneNumber= Integer
> 
> data Contact = Contact {name :: Name, phone:: PhoneNumber} deriving
> Show
> 
> -- The addressBook is a lista of Contacts
> data AddressBook= AddressBook [Contact] deriving Show
> 
> -- Create a test one
> ag = Agenda [Contact "BillG" 618965332, Contact "Linus" 5897458]
> 
> I wan't to write a function to search for a given phone by name and
> return a Maybe PhoneNumber.  I know how to do this recursively on a
> "raw" list, but how do I access the list inside AddressBook? Also,
> how do I represent an AddressBook with an empty list and one with
> data on its list for my pattern matching?
> 
> Thanks!

Sorry, I meant to write something along the lines of

bookHead :: AddressBook -> Contact
bookHead (AddressBook (c:cs)) = c

which isn't really useful.

I think you want to say

type AdressBook = [Contact]

instead.

-- 
(c) this sig last receiving data processing entity. Inspect headers for
past copyright information. All rights reserved. Unauthorised copying,
hiring, renting, public performance and/or broadcasting of this
signature prohibited. 



More information about the Haskell-Cafe mailing list