Exercise: values [✭]
What is the type and value of each of the following OCaml expressions?
7 * (1+2+3)
"CS " ^ string_of_int 3110
Hint: type each expression into the toplevel and it will tell you the answer. Note: ^
is not exponentiation.
int
string
Exercise: operators [✭✭]
Examine the table of all operators in the OCaml manual (you will have to scroll down to find it on that page).
- Write an expression that multiplies 42 by 10.
- Write an expression that divides 3.14 by 2.0. Hint: integer and floating-point operators are written differently in OCaml.
- Write an expression that computes 4.2 raised to the seventh power. Note: there is no built-in integer exponentiation operator in OCaml (nor is there in C, by the way), in part because it is not an operation provided by most CPUs.
Exercise: equality [✭]
- Write an expression that compares 42 to 42 using structural equality.
- Write an expression that compares "hi" to "hi" using structural equality. What is the result?
- Write an expression that compares "hi" to "hi" using physical equality. What is the result?
Exercise: assert [✭]
- Enter assert true;; into utop and see what happens.
- Enter assert false;; into utop and see what happens.
- Write an expression that asserts 2110 is not (structurally) equal to 3110.
The expression assert expr evaluates the expression expr and returns () if expr evaluates to true. If it evaluates to false the exception Assert_failure is raised with the source file name and the location of expr as arguments. Assertion checking can be turned off with the -noassert compiler option. In this case, expr is not evaluated at all.
Exercise: if [✭]
Write an if expression that evaluates to 42 if 2 is greater than 1 and otherwise evaluates to 7.
Exercise: double fun [✭]
Using the increment function from above as a guide, define a function double that multiplies its input by 2. For example, double 7 would be 14. Test your function by applying it to a few inputs. Turn those test cases into assertions.
Exercise: more fun [✭✭]
- Define a function that computes the cube of a floating-point number. Test your function by applying it to a few inputs.
- Define a function that computes the sign (1, 0, or -1) of an integer. Use a nested if expression. Test your function by applying it to a few inputs.
- Define a function that computes the area of a circle given its radius. Test your function with assert.
Exercise: date fun [✭✭✭]
Define a function that takes an integer d and string m as input and returns true just when d and m form a valid date. Here, a valid date has a month that is one of the following abbreviations: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec. And the day must be a number that is between 1 and the minimum number of days in that month, inclusive. For example, if the month is Jan, then the day is between 1 and 31, inclusive, whereas if the month is Feb, then the day is between 1 and 28, inclusive.
How terse (i.e., few and short lines of code) can you make your function? You can definitely do this in fewer than 12 lines.
标签:function,assert,your,Write,exercises,expression,2.6,Exercise,cs3110 From: https://www.cnblogs.com/sysss-blogs/p/17998060