Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.Collections;
import java.util.List;

public class Main {
    public static <A> List<A> reverse(List<A> as) {
        return reverse(as, Collections.EMPTY_LIST);
    }

    private static <A> List<A> reverse(List<A> as, List<A> acc) {
        return as.isEmpty() ? acc : reverse(tail(as), cons(head(as), acc));
    }

    public static <A> List<A> tail(List list) {
        List result = new ArrayList<>(list);
        result.remove(0);
        return Collections.unmodifiableList(result);
    }

    public static <A> List<A> cons(A a, List<A> as) {
        List result = new ArrayList<>(as);
        result.add(0, a);
        return Collections.unmodifiableList(result);
    }

    public static <A> A head(List<A> list) {
        return list.get(0);
    }
}