CSC173: Project 3 Functional ProgrammingJava

122 阅读3分钟

CSC173: Project 3Functional ProgrammingIn this unit, we are looking at Alan Turing’s formalization of “computable functions” using Turing machines. I have posted Turing’s famous paper On Computable Numbers (Turing, 1937a) on BlackBoard for you.The lambda (λ) calculus is an alternative formulation of computable functions developed by the mathematician Alonzo Church around the same time that Turing was developing his machine model (Church, 1936). The two formalizations have been proven equiva- lent, in that any function computable using one of them can be computed using the other. Turing actually discussed this in an appendix to his famous paper, and in a subsequent paper (Turing, 1937b). The Church-Turing thesis states that these and other equivalent formalisms completely define the informal notion of an “algorithm” or computable func- tion. So far no alternative model has been proposed that can compute anything that can’t be done with either Turing machines or the lambda calculus.Functional programming is based on the lambda calculus (at least in part). In this project, we will take a short break from C and do a bit of functional programming. However just like actually programming a Turing machine is quite involved, programming directly using the lambda calculus would be painful. Very, very painful. So instead, this term we will use the original functional programming language: Lisp.If you are new to Lisp, and most students will be, please check out the accompanying “Notes on Lisp for CSC173” that I have posted with this project description. It will take some time for you to get comfortable programming in Lisp. Be prepared to go to a study session if it is new to you.You should probably bookmark the Common Lisp HyperSpec, which documents all of Common Lisp (but is a reference, not a tutorial). However see Project Requirements, below, regarding which builtin features of Common Lisp you may use in this project.On Functional ProgrammingWhat is “functional” programming? Surely all programs should be “functional” in the sense of functioning properly. That is true. That’s also not what functional programming is about.Functional programming is a way of specifying how to compute something by giving its definition rather than by giving a set of steps that must be followed to perform the com- putation. This type of programming is called declarative, as opposed to the imperative languages like Java and Python with which you are already familiar. Computational def- initions are often recursive, so recursion is very widely used in functional programming.For example, take the problem of telling a computer how to add up the numbers from 1 to some number n. If you wrote the program in an imperative language like Java it would look something like this:int sum(代 写CSC173: Project 3 Functional ProgrammingJava int n) {int sum = 0;for (int i=1; i <= n; i++) {sum += i;}return sum;}As an alternative, try to think of how you would state the definition of the sum of the numbers from 1 ton, rather than describing the steps required to compute it.What’s the simplest case?Think about it. . .If n = 1, the sum of the numbers up ton is. . . 1, right?What about for numbers greater than 1?Think about it also. . .If n > 1, if you know the sum up to n − 1, and then the sum up to n is. . . that plus n, right? Note that this a recursive definition.In mathematical notation, we might write that as follows:In a functional programming language, the program will look just like the definition! Per- haps something like this:def sum(n):n = 1: 1n > 1: sum(n-1) + nNote that there is no “return” statement here. We’re not telling the computer what to do. If n is 1, the sum is 1. That’s what it says. If n > 1, the sum is something else and we’re given an expression for computing it.Of course, you could also write the Java method recursively:int sum(int n) {if (n == 1) return 1;if (n > 1) return sum(n-1) + n;}BTW: If you actually try this code, your Java compiler should complain. But you could fix it up.The key to functional programming is to think declaratively (“say what something is not how to compute it”). Very often that will involve recursion. What is a recursive definition of a list of, say, numbers? What is a recursive definition of the sum of a list of numbers? What does it mean to “filter” a list of numbers recursively to keep or remove some of them? That’s how you need to think in order to program functionally.All (almost all?) functional programming languages support some aspects of imperative progranmming. In fact, it is possible to write almost purely imperative programs using Lisp (or most practical functional programming languages). But that is not the goal for this project. Your code should be as purely functional as possible. For that reason, you are only allowed to use a subset of Lisp features, as described below. Here’s what Paul Graham, the founder of legendary tech incubator Y Combinator, has to say about learning to program in Lisp:For alumni of other languages, beginning to use Lisp may be like stepping onto a skating rink for the first time. It’s actually much easier to get around on ice than it is on dry land—if you use skates. Till then you will be left wondering what people see in this sport.What skates are to ice, functional programming is to Lisp. Together the two allow you to travel more gracefully, with less effort. But if you are accustomed to another mode of travel, WX:codehelp