Jump to content

Error in my recursive pseudo code

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Monster92

Monster92

    Newbie

  • Members
  • Pip
  • 2 posts
Ok I have written a program samelist(List1, List2). IT is supposed to check two lists for equality. However whenever I try to implement it in Java it doesn't work. A friend told me it is with the pseudo code I have written shown below. There's a mistake there but I cannot see it. Sorry again to be cheeky but if you find a way to correct can you provide an example of how it would work?


samelist(L1,L2) {  

if(L1==[] and L2==[]) return true;

	else return (head(L1)==head(L2) and samelist(tail(L1), tail(L2))); 

}


#2
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
I don't see any form of returning false in your psuedo code also you don't need to keep doing a return statement.
If end of list then return true
else If head of list a == head of list b then recursively call function with tail of lists
else return false

You basically end up using if statements as a type of pattern matching. When doing pattern matching you need to have a pattern for every logical potential.
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#3
Monster92

Monster92

    Newbie

  • Members
  • Pip
  • 2 posts
I'm apologize for my ignorance I don't see how that could be written. I'm new to the idea of pseudo code and programming at large




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users