Work on this exercise locally
This web app is a reference guide — you can read instructions, browse starter code, and view tests here. To actually complete the exercise, you need to work in your local development environment.
1Clone the repo:
git clone https://github.com/weihaoqu/program-analysis-bootcamp-student2Edit the starter file in your editor (VS Code, Vim, etc.) — replace
failwith "TODO" with your implementation.3Run the tests:
dune runtest modules/module0-warmup/exercises/ocaml-basicsOCaml Basics
3. Exercise 1: OCaml Basics (~20 min)
File: exercises/ocaml-basics/starter/main.ml
Run:
dune exec modules/module0-warmup/exercises/ocaml-basics/starter/main.exe
What You'll Implement
| Function | What It Does | Concept |
|---|---|---|
square | Returns x * x | Basic function |
is_empty | Checks if string is empty | String.length |
greet | Returns "Hello, name!" | String concatenation |
is_digit | Checks if char is '0'..'9' | Char comparison |
is_alpha | Checks if char is letter/underscore | Multiple ranges |
classify_char | Returns "digit"/"alpha"/"operator"/"unknown" | Using helper functions |
format_token | Formats a (category, text) tuple | Tuple destructuring |
make_token | Creates token from text string | String indexing |
format_pos | Formats a (line, col) position | Printf.sprintf |
advance_pos | Updates position after reading a char | Newline handling |
Hints
String.length sreturns the length ofs.s.[0]returns the first character of strings.Printf.sprintf "line %d, col %d" line colcreates a formatted string.- Characters can be compared with
<=:'0' <= c && c <= '9'.
Expected Output
=== Exercise 1: OCaml Basics ===
square 5 = 25
square (-3) = 9
is_empty "" = true
is_empty "hi" = false
greet "OCaml" = Hello, OCaml!
classify_char '7' = digit
classify_char 'x' = alpha
classify_char '+' = operator
classify_char '!' = unknown
format_token ("keyword", "if") = [keyword: if]
make_token "42" = [number: 42]
make_token "hello" = [identifier: hello]
make_token "+" = [symbol: +]
make_token "" = [empty: ]
format_pos (1, 1) = line 1, col 1
advance_pos (1,1) 'a' = line 1, col 2
advance_pos (1,3) '\n' = line 2, col 1
scan_positions "ab\ncd" = line 2, col 3
Done!
Starter Files
starter
starter/main.ml
Read-only
Loading editor...
Work on this exercise locally
This web app is a reference guide — you can read instructions, browse starter code, and view tests here. To actually complete the exercise, you need to work in your local development environment.
1Clone the repo:
git clone https://github.com/weihaoqu/program-analysis-bootcamp-student2Edit the starter file in your editor (VS Code, Vim, etc.) — replace
failwith "TODO" with your implementation.3Run the tests:
dune runtest modules/module0-warmup/exercises/ocaml-basicsOCaml Basics
3. Exercise 1: OCaml Basics (~20 min)
File: exercises/ocaml-basics/starter/main.ml
Run:
dune exec modules/module0-warmup/exercises/ocaml-basics/starter/main.exe
What You'll Implement
| Function | What It Does | Concept |
|---|---|---|
square | Returns x * x | Basic function |
is_empty | Checks if string is empty | String.length |
greet | Returns "Hello, name!" | String concatenation |
is_digit | Checks if char is '0'..'9' | Char comparison |
is_alpha | Checks if char is letter/underscore | Multiple ranges |
classify_char | Returns "digit"/"alpha"/"operator"/"unknown" | Using helper functions |
format_token | Formats a (category, text) tuple | Tuple destructuring |
make_token | Creates token from text string | String indexing |
format_pos | Formats a (line, col) position | Printf.sprintf |
advance_pos | Updates position after reading a char | Newline handling |
Hints
String.length sreturns the length ofs.s.[0]returns the first character of strings.Printf.sprintf "line %d, col %d" line colcreates a formatted string.- Characters can be compared with
<=:'0' <= c && c <= '9'.
Expected Output
=== Exercise 1: OCaml Basics ===
square 5 = 25
square (-3) = 9
is_empty "" = true
is_empty "hi" = false
greet "OCaml" = Hello, OCaml!
classify_char '7' = digit
classify_char 'x' = alpha
classify_char '+' = operator
classify_char '!' = unknown
format_token ("keyword", "if") = [keyword: if]
make_token "42" = [number: 42]
make_token "hello" = [identifier: hello]
make_token "+" = [symbol: +]
make_token "" = [empty: ]
format_pos (1, 1) = line 1, col 1
advance_pos (1,1) 'a' = line 1, col 2
advance_pos (1,3) '\n' = line 2, col 1
scan_positions "ab\ncd" = line 2, col 3
Done!