Tutorial by Examples: a

append([], Bs, Bs). append([A|As], Bs, [A|Cs]) :- append(As, Bs, Cs). append/3 is one of the most well-known Prolog relations. It defines a relation between three arguments and is true if the third argument is a list that denotes the concatenation of the lists that are specified in the firs...
CLP(FD) constraints are provided by all serious Prolog implementations. They allow us to reason about integers in a pure way. ?- X #= 1 + 2. X = 3. ?- 5 #= Y + 2. Y = 3.
emptyList = [] singletonList = [0] -- = 0 : [] listOfNums = [1, 2, 3] -- = 1 : 2 : [3] listOfStrings = ["A", "B", "C"]
listA = [1, 2, 3] listB = [4, 5, 6] listAThenB = listA ++ listB -- [1, 2, 3, 4, 5, 6] (++) xs [] = xs (++) [] ys = ys (++) (x:xs) ys = x : (xs ++ ys)
Sometimes you want to destructure key under a map which might not be present in the map, but you want a default value for the destructured value. You can do that this way: (def my-map {:a 3 :b 4}) (let [{a :a b :b :keys [c d] :or {a 1 c 2}} my-map] (println ...
Destructurling works in many places, as well as in the param list of an fn: (defn my-func [[_ a b]] (+ a b)) (my-func [1 2 3]) ;= 5 (my-func (range 5)) ;= 3 Destructuring also works for the & rest construct in the param list: (defn my-func2 [& [_ a b]] (+ a b)) (my-func2 1 ...
Destructuring also gives you the ability to interpret a sequence as a map: (def my-vec [:a 1 :b 2]) (def my-lst '("smthg else" :c 3 :d 4)) (let [[& {:keys [a b]}] my-vec [s & {:keys [c d]} my-lst] (+ a b c d)) ;= 10 It is useful for defining functions with named p...
Predicates that produce side effects leave the realm of pure logic. These are for example: writeq/1 read/1 format/2 Side effects are phenomena that cannot be reasoned about within the program. For example, deletion of a file or output on the system terminal.
Predicates that reason about instantiations are called meta-logical. Examples are: var/1 ground/1 integer/1 These predicates are outside the realm of pure monotonic logic programs, because they break properties like commutativity of conjunction. Other predicates that are meta-logical includ...
Predicates that reason about all solutions are extra-logical. These are for example: setof/3 findall/3 bagof/3
CLP(FD) constraints are completely pure relations. They can be used in all directions for declarative integer arithmetic: ?- X #= 1+2. X = 3. ?- 3 #= Y+2. Y = 1.
In oracle, the difference (in days and/or fractions thereof) between two DATEs can be found using subtraction: SELECT DATE '2016-03-23' - DATE '2015-12-25' AS difference FROM DUAL; Outputs the number of days between the two dates: DIFFERENCE ---------- 89 And: SELECT TO_DATE( '201...
Our example array: arr=(a b c d e f) Using a for..in loop: for i in "${arr[@]}"; do echo "$i" done 2.04 Using C-style for loop: for ((i=0;i<${#arr[@]};i++)); do echo "${arr[$i]}" done Using while loop: i=0 while [ $i -lt ${#arr[@]} ]; do...
The MEDIAN function since Oracle 10g is an easy to use aggregation function: SELECT MEDIAN(SAL) FROM EMP It returns the median of the values Works on DATETIME values too. The result of MEDIAN is computed by first ordering the rows. Using N as the number of rows in the group, Oracle calcula...
Following code will release the lock. There will be no problem. Behind the scenes lock statement works as try finally lock(locker) { throw new Exception(); } More can be seen in the C# 5.0 Specification: A lock statement of the form lock (x) ... where x is an expression of a referenc...
Partial application means calling a function with less arguments than it has and saving the result as another function (that waits for the rest of the arguments). multiplyBy: Int -> Int -> Int multiplyBy x y = x * y multiplyByTwo : Int -> Int -- one Int has disappeared! we ...
The function toupper will convert a string to upper case (capital letters). For example: BEGIN { greeting = "hello" loud_greeting = toupper(greeting) print loud_greeting } This code will output "HELLO" when run.
We create a new concurrent process by calling the spawn function. The spawn function will get as parameter a function Fun that the process will evaluate. The return value of the spawn function is the created process identifier (pid). 1> Fun = fun() -> 2+2 end. #Fun<erl_eval.20.52032458&gt...
Enum classes as any other classes can have a constructor and be initialized enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) }
Enum classes can also declare members (i.e. properties and functions). A semicolon (;) must be placed between the last enum object and the first member declaration. If a member is abstract, the enum objects must implement it. enum class Color { RED { override val rgb: Int = 0xFF0000 ...

Page 231 of 1099