# Recursion

Page 1 of 1

## 7 Replies - 1320 Views - Last Post: 07 April 2014 - 10:57 AM

### #1 abcd8989

Reputation: 0
• Posts: 3
• Joined: 07-April 14

# Recursion

Posted 07 April 2014 - 06:37 AM

Recently I learnt the basics of recursion and I find it extremely difficult for me. I have been asked to create a recursive function generating a list of intersection of two sorted list using pseudo code. Say, if the input lists, list1 and list2 are [1,2,4,5,9] and [2,3,6,9] , the function Intersect(list1, list2)would yield [2,9]. I have spent hours thinking about it and still, I am not sure what I should actually do. My brain gets lost in the recursive thinking style. Much obliged more any help!

Is This A Good Question/Topic? 0

## Replies To: Recursion

### #2 Skydiver

• Code herder

Reputation: 3921
• Posts: 12,426
• Joined: 05-May 12

## Re: Recursion

Posted 07 April 2014 - 06:52 AM

Show us the pseudo code that you have tried.

### #3 abcd8989

Reputation: 0
• Posts: 3
• Joined: 07-April 14

## Re: Recursion

Posted 07 April 2014 - 07:00 AM

```if (length(list1) = 0 or length(list2) =0 ) then return (I don't know...)

return 	ListIntersection(listTail(list1),list2))
else
return 	ListIntersection(list1,listTail(list2))

```

This post has been edited by macosxnerd101: 07 April 2014 - 07:07 AM
Reason for edit:: Please use code tags

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11072
• Posts: 41,538
• Joined: 27-December 08

## Re: Recursion

Posted 07 April 2014 - 07:08 AM

Is this a C/C++ question? If not, I will move it to a more appropriate forum.

### #5 abcd8989

Reputation: 0
• Posts: 3
• Joined: 07-April 14

## Re: Recursion

Posted 07 April 2014 - 07:09 AM

it is not, but i couldn't find a pseduo code forum so i put here , sorry about that><

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11072
• Posts: 41,538
• Joined: 27-December 08

## Re: Recursion

Posted 07 April 2014 - 07:15 AM

I'll move this to Software Development.

### #7 mojo666

Reputation: 375
• Posts: 811
• Joined: 27-June 09

## Re: Recursion

Posted 07 April 2014 - 09:45 AM

Quote

if (length(list1) = 0 or length(list2) =0 ) then return (I don't know...)

if one of the lists is empty, then there is no intersection, right? Just return an empty list.

Quote

You have determined that the head is part of your solution. So, shouldn't you concatenate that to the intersection of the tails of the two lists?

### #8 BBeck

• Here to help.

Reputation: 633
• Posts: 1,475
• Joined: 24-April 12

## Re: Recursion

Posted 07 April 2014 - 10:57 AM

If you're having trouble with recursion, it might be worth your while to go through my tutorial. It's written for C#, but recursion is recursion ... recursively.