L-99: Ninety-Nine Lisp Problems--or as I like to call it, I got 99 problems but a LISP ain't one--is a problem set designed to test prowess in Lisp (or Scheme, in my case), as well as algorithmic thinking. The problems are divided into subsets, each subset having a particular focus: lists, arithmetic, graphs, etc.
The first subset, comprised of problems 1 through 28, is the list-focused subset. The problems start easy; but, difficulty increases from there. Things get interesting around Problems 26 and 27, which deal with combinatoric set generation. I found Problem 27, in particular, to be challenging; and, it required a fair bit of thought to reach my current solution. The last two problems, Problems 28A and 28B, deal with sorting. I also found Problem 28B to be challenging; but, it was easier than Problem 27 to wrap my mind around. Perhaps the difference there is due simply to algorithm familiarity--which is good, when learning new things is the end game. Solutions.
Showing posts with label scheme. Show all posts
Showing posts with label scheme. Show all posts
Wednesday, August 13, 2008
Sunday, July 27, 2008
EoPL - Ch 1 - Exs 19-26
Exercises 19 through 26 in Chapter 1 of Essentials of Programming Languages begin delving into parsing concepts. In particular, they explore the concepts of free and bound variables in lambda expressions.
In the book, Figure 1.1 provides an implementation of the definition of occurring free and occuring bound (given by Definition 1.3.3). All the exercises are based on this definition; and, Exercises 22 through 26 focus on extending it.
Ex. 19
Define a function
Ex. 20
Give an example of a lambda calculus expression in which the same variable occurs free, but also has a value independent of its free occurence. Solution.
Ex. 21
Give an example of a lambda calculus expression in which the same variable occurs both free and bound. Solution: The solution for Exercise 20 also suffices for this exercise. In fact, I do not see much difference between the problems.
Ex. 22
Modify the functions
Ex. 23
Modify the functions
Ex. 24
Modify the functions
Ex. 25
Modify the functions
Ex. 26
Modify the functions
The inclusion of
In the book, Figure 1.1 provides an implementation of the definition of occurring free and occuring bound (given by Definition 1.3.3). All the exercises are based on this definition; and, Exercises 22 through 26 focus on extending it.
Ex. 19
Define a function
free-vars that takes a list structure representing a lambda calculus expression (as referenced by Definition 1.3.3), and returns a set of all variables that occur free within the expression. Similarly, define a function bound-vars that returns a set of all variables that occur bound in a given expression. Solution.Ex. 20
Give an example of a lambda calculus expression in which the same variable occurs free, but also has a value independent of its free occurence. Solution.
Ex. 21
Give an example of a lambda calculus expression in which the same variable occurs both free and bound. Solution: The solution for Exercise 20 also suffices for this exercise. In fact, I do not see much difference between the problems.
Ex. 22
Modify the functions
occurs-free? and occurs-bound? (see Figure 1.1) to allow lambda abstractions and applications with any number of parameters. Solution.Ex. 23
Modify the functions
occurs-free? and occurs-bound? (see Figure 1.1) to include if expressions. Solution.Ex. 24
Modify the functions
occurs-free? and occurs-bound? (see Figure 1.1) to include let and let* expressions. Solution.Ex. 25
Modify the functions
occurs-free? and occurs-bound? (see Figure 1.1) to include expressions of the form (quote [datum]). Solution.Ex. 26
Modify the functions
occurs-free? and occurs-bound? (see Figure 1.1) to include set! assignment expressions. Solution.The inclusion of
set! expressions in Exercise 26 is somewhat ambiguous: if a variable that was not in scope (i.e. free) is set!-ed does it become a bound variable? The actual Scheme interpreter only allows in-scope (i.e. bound) variables to be set!-ed, therefore set! never makes a bound variable from a free one. With that in mind, I chose to make my solution disregard appearance as the first operand in a set! statement as criteria for a bound variable.
Wednesday, July 9, 2008
EoPL - Ch 1 - Ex 18
Exercise 18 in Chapter 1 of Essentials of Programming Languages is our last Scheme introductory programming exercise. It broaches the topic of function composition.
Ex. 18, Pt. 1
Define a function
Ex. 18, Pt. 2
Define a function
Ex. 18, Pt. 3
Define a function
Ex. 18, Pt. 1
Define a function
compose : (f1 : 'b -> 'c) * (f2 : a' -> b') -> 'c that returns the composition of the functions f1 and f2, such that ((compose f g) x) is equivalent to (f (g x)). Solution.Ex. 18, Pt. 2
Define a function
car&cdr : (s : 'a) * (slist : ['a]) * (errvalue: 'b) -> [_] that returns an expression which, when evaluated, produces a function in turn. This expression should be comprised of car, cdr, and compose (see Pt. 1) applications only. The function generated by this expression, when given a list similar to slist, should return the element in the given list that is at the same position as the first occurrence of s in slist. If slist does not contain an instance of s, car&cdr should return errvalue instead of an expression. Solution.Ex. 18, Pt. 3
Define a function
car&cdr2 : (s : 'a) * (slist : ['a]) * (errvalue: 'b) -> [_] that returns an expression which, when evaluated, produces a function in turn. This expression should be comprised of car and cdr applications, and lambda expressions only. The function generated by this expression, when given a list similar to slist, should return the element in the given list that is at the same position as the first occurrence of s in slist. If slist does not contain an instance of s, car&cdr2 should return errvalue instead of an expression. Solution.
Sunday, June 29, 2008
EoPL - Ch 1 - Ex 17
Exercise 17 in Chapter 1 of Essentials of Programming Languages furthers the aims of Exercise 15 and Exercise 16. The subparts of Exercise 17 are more algorithmic in nature than the previous exercises, however. Namely, they touch upon sorting and binary tree traversal.
Ex. 17, Pt. 1
Define a function
Ex. 17, Pt. 2
Define a function
Ex. 17, Pt. 3
Define a function
Ex. 17, Pt. 1
Define a function
path : (n : int) * (bst : BinarySearchTree) -> [left|right] that returns a list, where each element is either left or right, that describes the path to the node in bst which contains n. If that node is the root node, return an empty list. Solution.Ex. 17, Pt. 2
Define a function
sort : (lon : [int]) -> [int] that returns a list like lon but with all elements sorted in increasing order. Solution.Ex. 17, Pt. 3
Define a function
sort : (predicate : int * int -> bool) * (lon : [int]) -> [int] that returns a list like lon but with all elements sorted according to the comparison predicate. Solution.
Wednesday, June 18, 2008
EoPL - Ch 1 - Ex 16
Exercise 16 in Chapter 1 of Essentials of Programming Languages builds on the progress of Exercise 15. It further explores programming with lists by delving into deeply-nested list structures (pun intended). Note that the
Ex. 16, Pt. 1
Define a function
Ex. 16, Pt. 2
Define a function
Ex. 16, Pt. 3
Define a function
Ex. 16, Pt. 4
Define a function
Ex. 16, Pt. 5
Define a function
up function in Part 1 plays counterpart to the down function from Exercise 15, Part 9.Ex. 16, Pt. 1
Define a function
up : (lst : [_]) -> [_] that returns a list like lst but with each list-element (element of type list) replaced by the elements that that list-element contains. Solution.Ex. 16, Pt. 2
Define a function
swapper : (s1 : 'a) * (s2 : 'a) * (lst : [_]) -> [_] that returns a list like lst but with all instances of s1 replaced by s2, and all instances of s2 replaced by s1. Solution.Ex. 16, Pt. 3
Define a function
count-occurrences : (s : 'a) * (slist : [_]) -> int that returns the number of times s occurs in slist, including occurrences nested arbitrarily deep in list-elements of slist. Solution.Ex. 16, Pt. 4
Define a function
flatten : (slist : [_]) -> [_] that returns a list of all non-list-elements in slist (including non-list-elements nested arbitrarily deep within list-elements) in depth-first-traversal order. Solution.Ex. 16, Pt. 5
Define a function
merge : (lon1 : [int]) * (lon2 : [int]) -> [int] that returns a list, in ascending order, of the elements in lon1 and in lon2, where lon1 and lon2 are each already sorted in ascending order themselves. Solution.
Sunday, June 8, 2008
EoPL - Ch 1 - Ex 15
Exercise 15 in Chapter 1 of Essentials of Programming Languages attempts to familiarize the reader with programming in the Scheme. It's split into ten parts that together provide a gentle introduction to working with lists, and to a lesser extent vectors, in a functional programming language.
Ex. 15, Pt. 1
Define a function
Ex. 15, Pt. 2
Define a function
Ex. 15, Pt. 3
Define a function
Ex. 15, Pt. 4
Define a function
Ex. 15, Pt. 5
Define a function
Ex. 15, Pt. 6
Define a function
Ex. 15, Pt. 7
Define a function
Ex. 15, Pt. 8
Define a function
Ex. 15, Pt. 9
Define a function
Ex. 15, Pt. 10
Define a function
Ex. 15, Pt. 1
Define a function
duple : (n : int) * (x : 'a) -> ['a] that returns a list containing n copies of x. Solution.Ex. 15, Pt. 2
Define a function
invert : (lst : [('a,'b)]) -> [('b,'a)] that returns a list like lst but with each pair element of reversed. Solution.Ex. 15, Pt. 3
Define a function
filter-in : (pred : 'a -> bool) * (lst : ['a]) -> ['a] that returns a list of the elements in lst for which pred succeeds. Solution.Ex. 15, Pt. 4
Define a function
every? : (pred : 'a -> bool) * (lst : ['a]) -> bool that returns false if pred fails for any element of lst, or otherwise returns true. Solution.Ex. 15, Pt. 5
Define a function
exists? : (pred : 'a -> bool) * (lst : ['a]) -> bool that returns true if pred succeeds for any element of lst, or otherwise returns false. Solution.Ex. 15, Pt. 6
Define a function
vector-index : (pred : 'a -> bool) * (v : #['a]) -> int|FALSE that returns the zero-based index of the first element in v for which pred succeeds, or returns false if no such element exists. Solution.Ex. 15, Pt. 7
Define a function
list-set : (lst : ['a]) * (n : int) * (x : 'a) -> ['a] that returns a list similar to lst, except that the n-th (using zero-based indexing) element is replaced with x. Solution.Ex. 15, Pt. 8
Define a function
product : (los1 : ['a]) * (los2 : ['b]) -> [('a, 'b)] that returns a list of pairs (in any order) that represents the Cartesian product of los1 and los2. Solution.Ex. 15, Pt. 9
Define a function
down : (lst : [_]) -> [[_]] that returns a list like lst but with each element wrapped in its own (single-element) list. Solution.Ex. 15, Pt. 10
Define a function
vector-append-list : (v : #['a]) * (lst : ['a]) -> #['a] that returns a new vector with the elements of v followed by the elements of lst. Do not use any built-in vector-to-list, list-to-vector, or append functions. Solution.
Subscribe to:
Posts (Atom)