Tutorial by Examples: cursive

Here we demonstrate how to process lists recursively using OCaml's pattern matching syntax. let rec map f lst = match lst with | [] -> [] | hd::tl -> (f hd)::(map f tl) In this case, the pattern [] matches the empty list, while hd::tl matches any list that has at least one element...
Recursive joins are often used to obtain parent-child data. In SQL, they are implemented with recursive common table expressions, for example: WITH RECURSIVE MyDescendants AS ( SELECT Name FROM People WHERE Name = 'John Doe' UNION ALL SELECT People.Name FROM Peopl...
1.6.5 git clone <url> --recursive Clones the repository and also clones all submodules. If the submodules themselves contain additional submodules, Git will also clone those.
for /r command can be used to recursively visit all the directories in a directory tree and perform a command. @echo off rem start at the top of the tree to visit and loop though each directory for /r %%a in (.) do ( rem enter the directory pushd %%a echo In directory: cd rem leave...
Vim macros can also be recursive. This is useful for when you need to act on every line (or other text object) till the end of the file. To record a recursive macro, start with an empty register. (A register can be emptied using q<register>q.) Choose a consistent starting point on each line ...
Given a local directory with the following contents: └── dir1 ├── subdir1 └── subdir2 We want to create the same subdir1, subdir2 under a new directory dir2, which does not exist yet. import os os.makedirs("./dir2/subdir1") os.makedirs("./dir2/subdir2") R...
Recursive type Discriminated unions can be recursive, that is they can refer to themselves in their definition. The prime example here is a tree: type Tree = | Branch of int * Tree list | Leaf of int As an example, let's define the following tree: 1 2 5 3 4 We can defi...
In Racket, we use recursion very frequently. Here is an example of a function that sums all of the numbers from zero to the parameter, n. (define (sum n) (if (zero? n) 0 (+ n (sum (sub1 n))))) Note that there are many helpful convenience based functions used here, such as ...
One method for creating recursive lambda functions involves assigning the function to a variable and then referencing that variable within the function itself. A common example of this is the recursive calculation of the factorial of a number - such as shown in the following code: lambda_factorial ...
Lists Lists can be defined as: data List a = Nil | Cons a (List a) If we translate this into our type algebra, we get List(a) = 1 + a * List(a) But we can now substitute List(a) again in this expression multiple times, in order to get: List(a) = 1 + a + a*a + a*a*a + a*a*a*a + ... ...
This example shows how to get every year from this year to 2011 (2012 - 1). WITH yearsAgo ( myYear ) AS ( -- Base Case: This is where the recursion starts SELECT DATEPART(year, GETDATE()) AS myYear UNION ALL -- This MUST be UNION ALL (cannot be UNION) -- Recurs...
Calculating the factorial of a number is a classic example of a recursive function. Missing the Base Condition: #include <stdio.h> int factorial(int n) { return n * factorial(n - 1); } int main() { printf("Factorial %d = %d\n", 3, factorial(3)); return 0;...
#define _XOPEN_SOURCE 500 #include <stdlib.h> /* for exit() */ #include <stdio.h> /* for remove() */ #include <ftw.h> /* for nftw() */ int unlink_cb( const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { return remove(fpath); } in...
#include <stddef.h> /* for offsetof() */ #include <stdlib.h> /* for exit() */ #include <stdio.h> /* for perror() */ #include <string.h> /* for strcmp() */ #include <unistd.h> /* for close(), unlink() */ #include <fcntl.h> /* for open() */ #incl...
Recursive use of make means using make as a command within a makefile. This technique is useful when a large project contains sub-directories, each having their respective makefiles. The following example will help understand advantage of using .PHONY with recursive make. /main |_ Makefile ...
When defining a recursively-expanded variable, the contents of the right-hand side are stored as-is. If a variable reference is present, the reference itself is stored (not the value of the variable). Make waits to expand the variable references until the variable is actually used. x = hello y =...
Although storing multiple values in a single column violates normalization rules, sometimes one has to deal with badly designed legacy tables. A recursive query can help convert comma-separated values into distinct rows. Create a sample badly designed table and insert some data: create table proje...
;;Recursively print the elements of a list (defun print-list (elements) (cond ((null elements) '()) ;; Base case: There are no elements that have yet to be printed. Don't do anything and return a null list. (t ;; Recursive case ;; Print the next elem...
Let's start with a simple algorithm to see how recursion could be implemented in Ruby. A bakery has products to sell. Products are in packs. It services orders in packs only. Packaging starts from the largest pack size and then the remaining quantities are filled by next pack sizes available. For ...
Matrix chain multiplication is an optimization problem that can be solved using dynamic programming. Given a sequence of matrices, the goal is to find the most efficient way to multiply these matrices. The problem is not actually to perform the multiplications, but merely to decide the sequence of t...

Page 2 of 3