grapes-where-grapes: Connects a recursion with sequences it should recurse over.

Description Usage Arguments Value Examples

Description

This function parses a dynamic programming recursion expression and evaluates it, returning the table that the recursions specify.

Usage

1
recursion %where% ranges

Arguments

recursion

Specification of the dynamic programming recursion.

ranges

Specification of the index-ranges the recursion should compute values over.

Value

A filled out dynamic programming table.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Fibonnaci numbers
fib <- {
  F[n] <- 1 ? n <= 2
  F[n] <- F[n-1] + F[n-2]
} %where% {
  n <- 1:10
}
fib

# Edit distance
x <- c("a", "b", "c")
y <- c("a", "b", "b", "c")
edit <- {
    E[1,j] <- j - 1
    E[i,1] <- i - 1
    E[i,j] <- min(
        E[i - 1,j] + 1,
        E[i,j - 1] + 1,
        E[i - 1,j - 1] + (x[i - 1] != y[j - 1])
    )
} %where% {
    i <- 1:(length(x) + 1)
    j <- 1:(length(y) + 1)
}
edit

dynprog documentation built on Dec. 9, 2019, 5:10 p.m.