# Write Yourself a Scheme in 48 Hours/Answers

| ←Conclusion | Write Yourself a Scheme in 48 Hours Answers |

# Chapter 1
### Exercise 2

The $ operator reduces the number of parentheses needed here. Alternatively you could write the function applications as read (args!!0).

### Exercise 3
# Chapter 2
## Section 3 - Parsing
### Exercise 1
#### Part 2

In order to answer this question, you need to do a bit of detective work! It is helpful to read up on do notation. Using the information there, we can mechanically transform the above answer into the following.
This can be cleaned up into the following:

### Exercise 2

We need to create a new parser action that accepts a backslash followed by either another backslash or a doublequote. This action needs to return only the second character.
Once that is done, we need to make some changes to parseString.

### Exercise 4

First, it is necessary to change the definition of symbol.
This means that it is no longer possible to begin an atom with the hash character. This necessitates a different way of parsing #t and #f.
This in turn requires us to make changes to parseExpr.
parseNumber need to be changed to the following.
And the following new functions need to be added.
Import the Numeric module to use the readOct and readHex functions.

### Exercise 5

The combination of anyChar and notFollowedBy ensure that only a single character is read.
Note that this does not actually conform to the standard; as it stands, "space" and "newline" must be entirely lowercase; the standard states that they should be case insensitive.

### Exercise 6

A possible solution for floating point numbers:
Furthermore, add
before parseNumber in parseExpr and the line
to the LispVal type.

### Exercise 7

Ratio, using Haskell's Rational type:
Additionally, import the Data.Ratio module, add
before parseNumber in parseExpr and the line
to the LispVal type.
Real is already implemented in the Float type from Exercise 6, unless I'm mistaken.
Complex using Haskell's Complex type:
As before, import the Data.Complex module, add
before parseNumber and parseFloat in parseExpr and the line
to the LispVal type.

## Section 4 - Recursive Parsers: Adding lists, dotted lists, and quoted datums
### Exercise 1

These two are analogous to parseQuoted:
Also add
to parseExpr.

### Exercise 2

I chose to go with Arrays as described in Data.Array and used list-array conversions for array construction.
In order to use this, import Data.Array and add the following to the LispVal type:
Add the following lines to parseExpr; before the parser for Lists and DottedLists.

### Exercise 3

This took a fair amount of fiddling with sepBy, endBy and friends. I started by getting the (. degenerate) dotted list to work and then went from there. This code tolerates trailing and leading spaces.
Another implementation using more advanced functions from the Parsec library. spaces is the one from this tutorial.
Alternative solution. spaces is the spaces from Parsec and spaces1 is the spaces from this tutorial.

# Chapter 3
### Exercise 1

Here is one way of adding a few of them.

### Exercise 3

Add symbol->string and string->symbol to the list of primitives, then:
This doesn't deal well with bad input, which is covered later.

# Chapter 5
### Exercise 2

Define a helper function that takes the equal/eqv function as an argument:
Now adjust the eqv clause:
And add clauses for List and DottedList to the equal function:

### Exercise 3
#### cond

Room for improvement here!
Another approach:
Yet another approach, piggy-backing off of the already-implemented if:
Yet another approach

#### case

This solution requires LispVal to have a deriving (Eq) clause, in order to use the `elem` function.

### Exercise 4

Let's add string-length and string-ref: