# Recursion

Page 1 of 1

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

### #1 abcd8989

• New D.I.C Head

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: 4784
• Posts: 15,811
• Joined: 05-May 12

## Re: Recursion

Posted 07 April 2014 - 06:52 AM

Show us the pseudo code that you have tried.
Was This Post Helpful? 0

### #3 abcd8989

• New D.I.C Head

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...)

if listHead(list1) = listHead(list2) then
return 	createList(listHead(list1),ListConcat(ListTail(list1, list2)

if listHead(list1) < listHead(list2) then
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

Was This Post Helpful? 0

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11765
• Posts: 44,209
• 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.
Was This Post Helpful? 0

### #5 abcd8989

• New D.I.C Head

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><
Was This Post Helpful? 0

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11765
• Posts: 44,209
• Joined: 27-December 08

## Re: Recursion

Posted 07 April 2014 - 07:15 AM

I'll move this to Software Development.
Was This Post Helpful? 0

### #7 mojo666

• D.I.C Addict

Reputation: 397
• Posts: 857
• 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

if listHead(list1) = listHead(list2) then
return createList(listHead(list1),ListConcat(ListTail(list1, list2)

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?
Was This Post Helpful? 0

### #8 BBeck

• Here to help.

Reputation: 777
• Posts: 1,865
• 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.
Was This Post Helpful? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}