📈
wiki
  • Introduction
  • Books
    • Technical books
      • Elements of Programming
    • Psychology books
      • Mindset
  • Computer Science
    • Data Structures
      • Skip Lists
    • Algorithms
      • Hashing
      • Eller's Algorithm
    • Formal verification
      • Coq
      • Lean
      • TLA+
    • Garbage Collection
  • Programming
    • Functional programming
      • Functional data structures
      • Algebraic Effects
      • Dependent Types
        • Agda
        • Idris
      • Continuations
      • Category Theory
      • Total functional programming
      • Uniqueness Typing
      • GADT
      • Tagless Final
    • Concurrent Programming
      • Mutex
      • Semaphores
    • Parallel Programming
    • Systems Programming
      • Tagged pointers
    • Unicode
  • Competitive Programming
  • Source control
    • Git
  • Programming languages
    • Java
      • Java Garbage Collection
      • Java Performance
      • Java Modules
      • Graal
      • Java libraries
        • Spring
          • Spring Cloud
        • Guava
        • JUnit 5
        • Hamcrest
        • Logging
        • Google Auto
        • Lombok
        • JOOQ
        • duct-tape
        • args4j
        • Netty
    • Scala
      • Dotty
      • Specialization
      • Benchmarking Scala
      • Scala libraries
        • ZIO
    • Haskell
      • Haskell tools
        • Haskell tools
    • Lisp
      • Lisp books
    • Rust
      • Rust Async
    • C++
      • C++ libraries
        • Boost
        • Poco
        • Abseil
        • facebook folly
        • Intell TBB
      • C++ tools
        • Valgrind
        • CMake
        • Bazel
      • C++ books
    • C
      • C libraries
      • C books
    • Python
      • Testing
      • Cython
      • Python performance
      • Python libraries
        • requests
        • httpx
        • boto3
      • Python tools
        • pyenv
        • Pipenv
        • mypy
      • Python Cookbook
    • Prolog
      • Prolog Cookbook
    • Clojure
      • Clojure Cookbook
    • Julia
      • Julia libraries
        • DataFrames
  • Networking
    • TCP
    • UDP
    • QUIC
  • Mindfullness
    • Mediatation
      • Samatha
      • Vipassana
    • Flow
  • LLVM
    • Clang tools
  • Operating systems
    • Securing operating systems
      • Capsicum
  • Linux
    • Packaging
    • RPM
    • Deblian
      • Linux tools
    • APT package manager
    • sxhkd
    • playerctl
    • fish
    • w3m browser
      • NixOS
    • Nix package manager
  • macOS
    • macOS tools
      • Hammerspoon
  • Windows
    • Windows tools
      • AutoHotkey
  • CLI
    • tmux
    • screen
    • ripgrep
    • fd
    • bat
    • tldr
    • install
    • chflags
  • Technology
    • Search
      • Google
      • DuckDuckGo
      • Algolia
      • Elastic search
  • Databases
    • MySQL
    • PostgreSQL
    • Oracle
  • Text editors
    • Emacs
      • Emacs plugins
      • Org mode
    • Vim
      • Vim plugins
      • Text objects
    • Sublime Text
  • IDEs
    • IntelliJ IDEA
      • IDEA plugins
        • IdeaVim
  • Philosophy
    • Stoicism
      • Parallels with Buddhism
    • Buddhist philosophy
  • Learning
    • Spaced Repetition
      • Anki
  • History
    • People
      • Benjamin Franklin
  • Buddhism
    • Theravada
      • Dhammapada
      • Eight Worldly Concerns
    • Mahayana
      • Zen
        • Zen Koans
  • Psychology
    • CBT
    • Cognitive biases
  • Logic
    • Classical logic
    • Intuitionistic logic
    • Logical fallacies
  • Literature
    • Western literature
    • Oriental literature
      • Bhagavad Gita
    • Mythology
    • Aphorisms
  • Research
    • Problem solving
    • Questioning
    • Blogs
  • Papers
    • Computer Science
      • Higher-order logic programming in Prolog
      • The Rise of Worse is Better
      • Advantages and Disadvantages of a Monolithic Repository
      • Coccinelle
      • Total Functional Programming
      • Scalable Component Abstractions
    • Psychology
    • Literature
      • Euclid and Pāṇini
  • Courses
    • Computer Science
  • Devops
    • Ansible
    • Puppet
    • TerraForm
  • Life
    • Happiness
    • Journaling
  • Meetups
    • Rust Meetups
      • LDN Talks January, 2020
    • Scala Meetups
      • Scala in the City - January, 2020
    • Papers We Love
      • CREST/PWL Special Event - January, 2020
  • Sports
    • Running
      • Running books
      • Running shoes
    • Chess
      • Chess openings
    • Hiking
      • Outdooraholics
        • Box Hill, Surrey
  • Music
    • Classical
      • Western classical music
      • Indian classical music
    • Popular music
    • Sinhala music
    • Indian music
  • Podcasts
  • Movies
  • TV Series
  • Log
    • 2019 logs
      • September
      • October
      • November
      • December
    • 2020 logs
      • January logs
      • February logs
      • March logs
      • April logs
      • May logs
      • June logs
  • TIL
    • TIL in 2020
      • January
      • February
      • March
      • April
      • May
      • June
      • October
      • November
Powered by GitBook
On this page
  1. Programming languages
  2. Prolog

Prolog Cookbook

PreviousPrologNextClojure

Last updated 5 years ago

  • Classic factorial function,

    factorial(0, 1).
    factorial(N, N_Fact) :-
        N > 0,
        M is N-1,
        factorial(M, M_Fact),
        N_Fact is M_Fact*N.
  • Fibonacci functions

    ;; Non tail recursive version - is going to stack overflow quickly
    fib(0, 0).
    fib(1, 1).
    fib(N, N_Fib) :-
        N > 1,
        M is N-1,
        T is N-2,
        fib(M, M_Fib),
        fib(T, T_Fib),
        N_Fib is M_Fib + T_Fib.
    
    ;; Tail recursive version
    fibonacci(N, N_Fib) :- tfib(N, 0, 1, N_Fib).
    tfib(0, A, _, A).
    tfib(N, A, B, N_Fib) :-
        N > 0,
        Next_N is N-1,
        Next_A is B,
        Next_B is A + B,
        tfib(Next_N, Next_A, Next_B, N_Fib).
  • Aggregate counting in SWI-Prolog,

    likes(jimmy, anna).
    likes(paul, anna).
    likes(jimmy, paul).
    
    popular(X) :- aggregate(count, Y^likes(Y,X), N), N > 1.

    Here, popular function checks whether given person is liked by more than one person. In the definition Y is existentionally qualified.

    References -

  • Higher order functions,

    double(X, Y) :- Y is X + X.
    pow2(X, Y) :- Y is X * X.
    
    map([], _, []).
    map([X|Xs], P, [Y|Ys]) :-
      call(P, X, Y),
      map(Xs, P, Ys).
    
    ;; Examples
    map([1,  2, 3], double, Xs). ;; Xs = [1, 4, 6]
    map([1, -2, 3],   pow2, Xs). ;; Xs = [1, 4, 9]
  • Checking whether a predicate exists - .

    current_predicate(map/3). ;; checks whether map which takes 3 parameters exists
  • Define contains based on append function,

    append([], Ys, Ys).
    append([X|Xs], Ys, [X|Zs]) :- append(Xs, Ys, Zs).
    
    prefix(P, L) :- append(P, _, L).
    suffix(S, L) :- append(_, S, L).
    
    contains(SubL, L) :- suffix(S, L), prefix(SubL, S), !.
  • Palindrome check in Prolog is quite nice,

    palindrome(Xs) :- reverse(Xs, Xs).

    In a restricted set, we can even use this declarative definition to generate palindromes, as in the following example!

    member(X, [1, 2, 3]),
    member(Y, [10, 11, 12]),
    member(Z, [1, 2]),
    palindrome([X, Y, Z]).
  • Fizz buzz in Prolog,

    ```prolog print_fizz_buzz(N) :- ( 0 is mod(N, 15) -> write("fizzbuzz"),nl ; 0 is mod(N, 3) -> write("fizz"), nl ; 0 is mod(N, 5) -> write("buzz"), nl ; write(N), nl ).

fizz_buzz(N) :- aux_fizz_buzz(0, N). aux_fizz_buzz(M, N) :- M < N, print_fizz_buzz(M), M1 is (M + 1), aux_fizz_buzz(M1, N). aux_fizz_buzz(M, N) :- M >= N, !, nl.

- `current_op` can be used to find out precedence and type of an operator.
  Example, to find out these information about `mod`,

  ```prolog
  current_op(Precedence, Type, mod).

In true Prolog fashion, one can find out all the operators which are curently definied using something like follows,

  current_op(Precedence, Type, Op).

which will list all the opeartors with their respective precedence and type.

  • Permutations,

    take([H|T], H, T).
    take([H|T], R, [H|S]) :- take(T, R, S).
    
    perm([], []).
    perm(List, [H|T]) :- take(List, H, R), perm(R, T).
https://stackoverflow.com/questions/5930340/aggregate-3-in-swi-prolog/5930420
https://stackoverflow.com/questions/12886179/prolog-how-to-check-if-a-predicate-exists