SLIDE 8 70
- 14. FUNCTIONAL PROGRAMMING
There is only one remaining problem. The replaceAll function generates an extra [ ], giving [[[Int]]] instead of the desired [[Int]]. Two possible solutions for this problem are:
- a. Combine replaceOne and replaceAll into one function.
This is shown by the function add in the program sudoku1.hs
- b. Flatten the lists. This is shown in the program sudoku2.hs
To solve a sample puzzle, load one of these programs into the Haskell interpreter and then make the call: sudoku easy1 at the prompt. To solve other puzzles than easy1, change the argument to this call. 14.32 Some of the abstract syntax for Clite is modeled in Haskell the file Clite.hs, which has solutions to Exercises 14.23-14.25. Use Appendix A for a complete definition of the Clite type checking functions. The Clite type map can be modeled as follows: type Typemap = [(Variable, Type)] type Variable = String type Type = "int" | "boolean" | "char" | "float" The function typing needs Haskell definitions of the abstract concepts
- f Declaration and Program, and can be defined as a function. All the
validity functions can be defined using these definitions together with a complete Haskell recursive data type definition of Clite abstract syntax. 14.33 Most of this is summarized in Section 14.3.8, along with the file Clite.hs which has solutions to Exercises 14.23-14.25. The challenge here is to complete the implementation of the Clite lnterpreter in Haskell and then demonstrate its completeness by interpreting a variety of abstract Clite
- programs. Use Appendix A for a complete definition of the Clite meaning
functions. 14.34 This problem is reminiscent of a classical exercise in functional program- ming where students built a Scheme interpreter in Scheme. Usually a small subset of the Scheme function library was implemented, including functions cons, car, cdr, conditional expressions, and function definition and call. Fortunately, Scheme syntax is simple, and can be defined in just a few BNF rules. Tokens in Scheme are either parentheses, symbols, or num-
- bers. Thus, the abstract syntax is also simple. Implementing the run-time
semantics of Scheme in Java requires the design of a read-eval-print loop. At each iteration, the user either calls or defines a Scheme function and