Skip to content

Instantly share code, notes, and snippets.

@moozzyk
Last active January 28, 2018 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moozzyk/565d7c74bd3af3b14b3c1306d6423aa2 to your computer and use it in GitHub Desktop.
Save moozzyk/565d7c74bd3af3b14b3c1306d6423aa2 to your computer and use it in GitHub Desktop.
~~ Verbose https://www.reddit.com/r/adventofcode/comments/7hvtoq/2017_day_6_solutions/dqu8z8v/ (vs mine) ~~
~~Inconsistencies (fold - accumulator first in List.fold_left but second in Hashtbl)
print_int (List.fold_left (fun acc line -> max acc (process line registers)) min_int lines);
print_endline "";
print_int (Hashtbl.fold (fun k v acc -> if v > acc then v else acc) registers min_int);~~
Documentation
- lacking, samples very hard to find
- http://caml.inria.fr/pub/docs/manual-ocaml/libref/Str.html - initally confusing than helpful
~~Regular expressions - match groups~~
~~Basis IO operations - too complicated/missing~~
~~Basic text/string processing - hard/takes a lot of time~~
Errors:
* additional semicolon
* begin - end needed for if even if has semicolon
* Syntax error
```
File "solution.ml", line 49, characters 0-3:
Error: Syntax error
Command exited with code 2.
Compilation unsuccessful after building 1 target (0 cached) in 00:00:00.
```
* missing "in"
* using `=>` instead of `->`
* using reserved words `val` `match` etc.
* too many parameters due to lack of parenthesis
* missing parameter - value converted to function impossible to find (how to repro? Day 7)
* math expressions have to be in () (even though in samples they don't seem to have to be in ())
match_list line group + 1
Error: This expression has type int but an expression was expected of type
'a list
match_list line (group + 1)
Compiles fine
* Error: Syntax error: pattern expected.
missing `->` in match
* Unbound value test
- the value does not exist (e.g. typo in name)
- recursive function not marked with `rec`
* list concat:
print_list right@left fails
Error: This expression has type unit but an expression was expected of type
'a list
print_list (right@left) works
* abs requires brackets when passed as argument (and maybe if not?) e.g. `(abs(5))` and not `abs(4)` (Day11)
* exceptions are lacking details (e.g. stack trace): ```Fatal error: exception Invalid_argument("index out of bounds")```
* Passing negative value: (Day22)
print_state infected_cells -5 5 row col; produces
File "solution.ml", line 48, characters 4-30:
Error: This expression has type int -> int -> int -> int -> unit
but an expression was expected of type int
`print_state infected_cells (-5) 5 row col;` - works
* beware float to int conversion (Day 23):
let is_prime n =
let rec is_prime_aux n div =
(* if div > int_of_float(sqrt (float_of_int n)) then *)
if div > n then
true
else
if n mod div = 0 then false else is_prime_aux n (div + 1)
in
is_prime_aux n 2
works
`if div > sqrt n then`
File "solution.ml", line 62, characters 9-10:
Error: This expression has type float but an expression was expected of type
int
(note - error on a completely different line)
`if div > int_of_float (sqrt n) then`
Same error
`if div > int_of_float (sqrt (float_of_int (n))) then`
works but is there a better way?
* List concat (Day 24)
match Hashtbl.find_opt parts key with
| Some v -> Hashtbl.replace parts key v@[value]
| None -> Hashtbl.add parts key [value]
File "solution.ml", line 18, characters 16-43:
Error: This expression has type unit but an expression was expected of type
'a list
Command exited with code 2.
change to ` | Some v -> Hashtbl.replace parts key (v@[value])` - compiles
links to sources
sample code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment