Hacker News new | ask | show | jobs
by ruv 4752 days ago
You mention doing it for variable length chains, It requires hardly more effort than hard coding the length, something like

    find_match(X, P) :-
        append([usd | _], [usd], X),
        profit(X, P),
        P > 1.

    profit([A, B], P) :- exchange(A, B, P).
    profit([A, B | T], P) :-
        exchange(A, B, P1),
        profit([B | T], P2),
        P is P1 * P2.
 
    # 1 ?- find_match(X, P).
    # X = [usd, jpy, usd],
    # P = 1.0040982 ;
    # X = [usd, eur, jpy, usd],
    # P = 1.0040882716200001 ;
    # X = [usd, eur, btc, usd],
    # P = 1.01209651875 ;
    # X = [usd, btc, jpy, usd],
    # P = 1.0025512896 ;
    # X = [usd, eur, usd, jpy, usd],
    # P = 1.003776175666278 .
1 comments

Thanks for sharing this. The way these are defined reminds me of the way I'd be defining methods in SML - a base case and then a recursive one to deal with the tail.