Created
January 21, 2017 07:09
-
-
Save ezksd/07a91873fcacdf66e44ed7aca7171b17 to your computer and use it in GitHub Desktop.
eight queens in a line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package core; | |
import java.util.function.BiFunction; | |
import java.util.function.Function; | |
public class EightQueens { | |
static <A, B> A car(Pair<A, B> pair) { | |
return pair.a; | |
} | |
static <A, B> B cdr(Pair<A, B> pair) { | |
return pair.b; | |
} | |
static <E> List<E> cons(E item, List<E> list) { | |
return new List<>(item, list); | |
} | |
static <E> List<E> list(E... items) { | |
List<E> r = null; | |
for (int i = items.length - 1; i >= 0; i--) { | |
r = new List<>(items[i], r); | |
} | |
return r; | |
} | |
static <E> List<E> append(List<E> l1, List<E> l2) { | |
return l1 == null ? l2 : cons(car(l1), append(cdr(l1), l2)); | |
} | |
static <T, R> R apply(Function<T, R> f, T t) { | |
return f.apply(t); | |
} | |
static <T, U, R> R apply(BiFunction<T, U, R> op, T first, U second) { | |
return op.apply(first,second); | |
} | |
static <T, R> List<R> map(Function<T, R> f, List<T> list) { | |
return list == null ? null : cons(apply(f, car(list)), map(f, cdr(list))); | |
} | |
static <E> List<E> filter(Function<E, Boolean> pred, List<E> list) { | |
return isNull(list)?null:apply(pred, car(list)) ? cons(car(list), filter(pred, cdr(list))) : filter(pred, cdr(list)); | |
} | |
static <T,R> R accumulate(BiFunction<R, T, R> op, R initial, List<T> list) { | |
return list == null ? initial : accumulate(op, apply(op, initial, car(list)), cdr(list)); | |
} | |
static <T, R> List<R> flatmap(Function<T, List<R>> op, List<T> list) { | |
return accumulate(EightQueens::append, null, map(op, list)); | |
} | |
static List<Integer> range(int start,int end){ | |
return start>end?null:cons(start, range(start + 1, end)); | |
} | |
static <A, B, C, D> TriFunction<A, B, C, D> y(Function<TriFunction<A, B, C, D>, TriFunction<A, B, C, D>> x) { | |
RecurFunc<TriFunction<A,B,C,D>> prod = f -> x.apply((a,b,c) -> f.apply(f).apply(a,b,c)); | |
return prod.apply(prod); | |
} | |
static int count(List<?> list){ | |
return isNull(list)? 0:1 + count(cdr(list)); | |
} | |
static boolean isNull(Object o) { | |
return o == null; | |
} | |
public static void main(String[] args) { | |
System.out.println(count(accumulate((r, i) -> flatmap((List<Integer> ps0) -> map(y -> cons(y, ps0), filter(y0 -> y((TriFunction<List<Integer>, Integer, Integer, Boolean> fn) -> (List<Integer> ps, Integer y, Integer gap) -> ps == null || !(car(ps) == y || car(ps) == y + gap || car(ps) == y - gap) && fn.apply(cdr(ps), y, gap + 1)).apply(ps0, y0, 1), range(1, 8))), r), list((List<Integer>) null), range(1, 8)))); | |
} | |
} | |
class Pair<A, B> { | |
A a; | |
B b; | |
Pair(A a, B b) { | |
this.a = a; | |
this.b = b; | |
} | |
} | |
class List<E> extends Pair<E, core.List<E>> { | |
List(E e, core.List<E> eList) { | |
super(e, eList); | |
} | |
} | |
interface TriFunction<A,B,C,R>{ | |
R apply(A a, B b, C c); | |
} | |
interface RecurFunc<T> extends Function<RecurFunc<T>, T> {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment