364 search results for "function"
-
How to Work with the Garbage Collector
The Gc Module
.ml --> <!-- TODO: Probably write a GC example without dependencies --> Here is a program that runs and then prints out GC statistics just before quitting: The Gc module contains some useful functions for querying and calling the garbage collector from OCaml programs.
Guides -
How to Work with the Garbage Collector
Exercises
xtend the program so it acquires a read lock on getting the record, but upgrades this to a write lock just before the user updates any field. Support a variable number of records , and add a function to create a new record (in the file). [Tip: OCaml has support for weak hashtables.] Add support for variable-length records . Make the underlying file representation a DBM-style hash . Provide
Guides -
Command-line Arguments
Sys.argv
ndard library, therefore its full name is Sys.argv . The number of arguments including the name of the program itself is simply the length of the array. It is obtained using the Array.length function.
Tutorials -
Transitioning to Multicore with ThreadSanitizer
Address the Reported Races and Rerun the Tests, Take 2 (Steps 3 and 2)
ml 5.x parallelism, hurrah! We can now rerun our tests under TSan to confirm the fix: Oh, wait! When raising an exception in transfer , we forgot to unlock the Mutex again. Let's adapt the function to do so:
Guides -
Transitioning to Multicore with ThreadSanitizer
Final Remarks and a Word of Warning
l fix as follows: The programming pattern of 'always-having-to-do-something-at-the-end' that we encountered with the missing Mutex.unlock is a recurring one for which OCaml offers a dedicate function:
Guides -
File Manipulation
Writing
Standard out_channel s: stdout , stderr Commonly used functions: open_out , open_out_bin , flush , close_out , close_out_noerr Open the file to obtain an out_channel Write to the channel If you want to force writing to the physical device, you must flush
Tutorials -
File Manipulation
Reading
Standard in_channel : stdin Commonly used functions: open_in , open_in_bin , close_in , close_in_noerr Open the file to obtain an in_channel Read characters from the channel. Reading consumes the channel, so if you read a character, the chan
Tutorials -
OCaml Programming Guidelines
Usage in Module Interface
The function's usage must appear in the module's interface that exports it, not in the program that implements it. Choose comments as in the OCaml system's interface modules, which will subsequently automatic
Resources -
OCaml Programming Guidelines
Use Assertions
is verified upon each execution, while a comment can quickly become obsolete, making it detrimental to understanding the program. <!-- $MDX skip --> For example, the conditions to validate a function's arguments are usefully verified by assertions. Use assertions as much as possible, as they let you avoid verbose comments while allowing a useful verification upon execution.
Resources -
OCaml Programming Guidelines
Don't use abbreviations for global names
Global identifiers (including the names of functions) can be long because it's important to understand what purpose they serve far from their definition.
Resources -
OCaml Programming Guidelines
Subdividing into modules
For each interface, you must document the things defined by the module: functions, types, exceptions, etc. For each module, you must explicitly write an interface. You must subdivide your programs into coherent modules.
Resources -
OCaml Programming Guidelines
Single branches
If cond , e1 , and e2 are small, simply write them on one line: If the expressions making up a conditional are purely functional (without side effects), we advocate binding them within the conditional by using let e = ... in when they're too big to fit on a single line. Justification : This way you get back the sim
Resources -
Comparison of Standard Containers
Lists: Immutable Singly-Linked Lists
Not very efficient for: random access, indexed elements Well-suited for: I/O, pattern-matching Adding an element: O(1) , cons operator :: Length: O(n) , function List.length Accessing cell i : O(i) Finding an element: O(n) Adding an element always creates a new list l from an element x List tl . tl remains unchanged, but it is not copied either.
Resources -
Comparison of Standard Containers
Arrays: Mutable Vectors
of elements of known size, accessing elements by numeric index, and modifying in-place elements. Basic arrays have a fixed length. Adding an element (by creating a new array): O(n) Length: O(1) , function Array.length Accessing cell i : O(1) Finding an element: O(n) Arrays are mutable data structures with a fixed length and random access.
Resources