4 ms·
I'm surprised Prolog is not listed there. here's a simple example, try designing a function that calculates Fibonacci two ways in your favourite language. giv
by segmondy 12y ago
I'm surprised Prolog is not listed there. here's a simple example, try designing a function that calculates Fibonacci two ways in your favourite language. given fib(N,X). You can supply either N, or X or neither and generate the result.
?- time(fib(N, 703303677114228158218352548771835497701812698363587327426049
050871545371181969335797422494945626117334877504492417659910
881863632654502236471060120533741212738673391111981393731255
98767690091902245245323403501)).
% 44,649,335 inferences, 4.182 CPU in 4.193 seconds (100% CPU, 10676854 Lips)
N = 1000 .
?- time(fib(1000, F)).
% 497,690 inferences, 0.072 CPU in 0.072 seconds (100% CPU, 6935880 Lips)
...
- ctaylor001 12y agoHaskell - fib (Just n) Nothing = Just (fibs !! n) fib Nothing (Just x) = let (n, x') = head (filter (\(_,y) -> y >= x) (enumerate fibs)) in if x == x' then Just n else Nothing fibs = 1 : 1 : zipWith (+) fibs (tail fibs) enumerate = zip [0..] I think that should work! It's a bit undefined what happens if you supply both arguments, or neither. It would be better to use Either Integer Integer as the input instead of a pair of Maybe Integer. Of course there's always the LogicT monad if you want to do it "properly".
- segmondy 12y agothe same prolog problem is defined for what happens when you supply both arguments, if the statement is true it will eval to true, if it is false, it will eval to false. if neither, it will generate all possible solutions, one at a time. :-use_module(library(clpq)). fib_aux(N, A, Sum,, F) :- { N = 0, F = 1}; { N = 1, F = 1}; { N >= 2, N1 = N - 1, Acc = Sum, NewSum = A + Sum, F = F1 + Acc, F1 >= N1 % to not run infinitly for unsatisfiable question clp_fib(N, 4). }, fib_aux(N1, Acc, NewSum, F1). fib(N, F) :- fib_aux(N, 0, 1, F).
- pjmlp 12y agoProlog is quite nice, it is the next step for those that already grok functional programming.
- agumonkey 12y agoBeside dependent types, bidirectional (~relational) programming is the only thing that amaze me. Dan Friedman gave talks about [mini]kanren, see https://www.youtube.com/watch?v=5Q9x16uIsKA https://www.youtube.com/watch?v=5Q9x16uIsKA I couldn't find the one where he asks (eval exp '()) to be 6 and gets many results such as 6 ;; duh (let ((_ 5)) ((lambda (x) (+ x 1)) _)) ;; better Here's an example involving cons cells only https://www.youtube.com/watch?v=kS_2_-E_JjU https://www.youtube.com/watch?v=kS_2_-E_JjU
- mattmight 12y agoI'll be updating the article at some point to add miniKanren. Will Byrd is my postdoc, and he's been pushing relational/logic programming with miniKanren: http://minikanren.org/ http://minikanren.org/ If you haven't seen miniKanren, check it out.