Skip to content

Instantly share code, notes, and snippets.

@philipschwarz
Created October 29, 2011 20:45
Show Gist options
  • Save philipschwarz/1325074 to your computer and use it in GitHub Desktop.
Save philipschwarz/1325074 to your computer and use it in GitHub Desktop.
Recursive multiplyByTwo
public class Main
{
public static void main(String[] args)
{
List list = List.EMPTY_LIST;
int n = 6000;
while(n > 0)
list = list.cons(n--);
list = multiplyByTwo(list);
}
private static List multiplyByTwo(List list)
{
if(list.isEmpty())
{
return list;
}
else
{
return multiplyByTwo(list.tail()).cons(2 * list.head());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment