You are previewing F# for Scientists.
2. Foreword
3. Preface
4. Acknowledgments
5. List of Figures
6. List of Tables
7. Acronyms
8. 1. INTRODUCTION
1. 1.1. PROGRAMMING GUIDELINES
2. 1.2. A BRIEF HISTORY OF F#
3. 1.3. BENEFITS OF F#
4. 1.4. INTRODUCING F#
5. 1.5. IMPERATIVE PROGRAMMING
6. 1.6. FUNCTIONAL PROGRAMMING
9. 2. PROGRAM STRUCTURE
1. 2.1. NESTING
2. 2.2. FACTORING
3. 2.3. Modules
4. 2.4. OBJECTS
5. 2.5. FUNCTIONAL DESIGN PATTERNS
6. 2.6. F# DEVELOPMENT
10. 3. DATA STRUCTURES
1. 3.1. ALGORITHMIC COMPLEXITY
2. 3.2. ARRAYS
3. 3.3. LISTS
4. 3.4. SETS
5. 3.5. HASH TABLES
6. 3.6. MAPS
7. 3.7. CHOOSING A DATA STRUCTURE
8. 3.8. Sequences
9. 3.9. Heterogeneous Containers
10. 3.10. TREES
11. 4. NUMERICAL ANALYSIS
1. 4.1. NUMBER REPRESENTATION
2. 4.2. ALGEBRA
3. 4.3. INTERPOLATION
4. 4.4. QUADRATIC SOLUTIONS
5. 4.5. MEAN AND VARIANCE
6. 4.6. OTHER FORMS OF ARITHMETIC
12. 5. INPUT AND OUTPUT
1. 5.1. PRINTING
2. 5.2. GENERIC PRINTING
3. 5.3. READING FROM AND WRITING TO FILES
4. 5.4. SERIALIZATION
5. 5.5. LEXING AND PARSING
13. 6. SIMPLE EXAMPLES
1. 6.1. FUNCTIONAL
2. 6.2. NUMERICAL
3. 6.3. STRING RELATED
4. 6.4. LIST RELATED
5. 6.5. ARRAY RELATED
6. 6.6. HIGHER-ORDER FUNCTIONS
14. 7. VISUALIZATION
1. 7.1. WINDOWS FORMS
2. 7.2. MANAGED DIRECTX
3. 7.3. TESSELATING OBJECTS INTO TRIANGLES
15. 8. OPTIMIZATION
1. 8.1. TIMING
2. 8.2. PROFILING
3. 8.3. ALGORITHMIC OPTIMIZATIONS
4. 8.4. LOWER-LEVEL OPTIMIZATIONS
16. 9. LIBRARIES
2. 9.2. CHARTING AND GRAPHING
4. 9.4. RANDOM NUMBERS
5. 9.5. REGULAR EXPRESSIONS
6. 9.6. VECTORS AND MATRICES
8. 9.8. COMPRESSION
9. 9.9. HANDLING XML
10. 9.10. CALLING NATIVE LIBRARIES
11. 9.11. FOURIER TRANSFORM
12. 9.12. METAPROGRAMMING
17. 10. DATABASES
1. 10.1. PROTEIN DATA BANK
2. 10.2. WEB SERVICES
3. 10.3. RELATIONAL DATABASES
18. 11. INTEROPERABILITY
1. 11.1. EXCEL
2. 11.2. MATLAB
3. 11.3. MATHEMATICA
19. 12. COMPLETE EXAMPLES
1. 12.1. FAST FOURIER TRANSFORM
2. 12.2. SEMI-CIRCLE LAW
3. 12.3. FINDING N TH-NEAREST NEIGHBORS
4. 12.4. LOGISTIC MAP
5. 12.5. REAL-TIME PARTICLE DYNAMICS
20. A. Troubleshooting
21. Glossary
22. Bibliography

# Appendix A. Troubleshooting

This appendix details many of the problems commonly encountered by new F# programmers.

# Value Restriction

One problem encountered when using an F# interactive session is the value restriction, which requires types to be completely defined.

The simplest example impeded by the value restriction is the definition of a reference to an empty list:

```> let x = ref [];;
----^^stdin(2,4): error: FS0030: Value restriction.
Type inference has inferred the signature
val x : '_a list ref
Either define 'x' as a simple data term, make it a```
`function, or add a type constraint to instantiate the type parameters.`

The empty list has the well-defined type 'a `list` but a reference to the empty list does not have a well-defined type because the empty list might be replaced by a non-empty list, in which case the elements must be of a specific type, the type of which has not been defined. In this case, the value restriction protects the programmer from accidentally setting the reference to an `int list` in one part of the program but a `float list` in another part of the program.

This problem can always be addressed by adding a type annotation to constrain the type of the value. In this case, we might know that the list will contain `int` values, so we annotate accordingly to make the F# interactive session happy:

```> let x : int list ref = ref [];;
val x : int list ref```

The problem occurs mainly when executing parts of a program being developed in an F# interactive session.