Page 1 of 1

## 3 Replies - 1111 Views - Last Post: 14 February 2017 - 10:49 AM

### #1 NoYouDidnt

Reputation: 5
• Posts: 55
• Joined: 04-July 15

Posted 14 February 2017 - 06:21 AM

I have the following half-implemented functions and types and trying to make a phone book.

```type Name = String
type Pnum = Integer
type Pbok = Name -> Maybe Pnum

lookup' :: Pbook -> Name -> Maybe Pnum
lookup' f x = f x

insert :: Pbook -> Name -> Pnum -> Pbook
insert pb x n =
if (length . show) n /= 8
then error "Expected phone number with 8 digits."
else (\z -> if z == x then Just n else Nothing)

delete :: Pbook -> Name -> Pbook
delete f x = (\x -> Nothing)

```

The problem is that I know how to pass around the phone book that contains the names. Any help is appreciated!

Is This A Good Question/Topic? 0

## Replies To: Haskell Phone Book

### #2 sepp2k

• D.I.C Lover

Reputation: 2756
• Posts: 4,416
• Joined: 21-June 11

Posted 14 February 2017 - 07:24 AM

```else (\z -> if z == x then Just n else Nothing)
```

This is not correct. You're creating a new phone book that contains the mapping x -> n and nothing else. You'll want to use the old phone book (i.e. pb) for keys other than x, instead of just returning Nothing.

### #3 NoYouDidnt

Reputation: 5
• Posts: 55
• Joined: 04-July 15

Posted 14 February 2017 - 09:45 AM

sepp2k, on 14 February 2017 - 02:24 PM, said:

```else (\z -> if z == x then Just n else Nothing)
```

This is not correct. You're creating a new phone book that contains the mapping x -> n and nothing else. You'll want to use the old phone book (i.e. pb) for keys other than x, instead of just returning Nothing.

Ok so

```(\z -> if z == x then Just n else pb z)
```

Thank you!

### #4 sepp2k

• D.I.C Lover

Reputation: 2756
• Posts: 4,416
• Joined: 21-June 11