(OCamlでは)このソリューションは
let compress l =
let rec compress_2 l e =
match l with
| [] -> [e]
| h::t -> if (h=e)
then (compress_2 t e)
else e::(compress_2 t)
in
match l with
| [] -> []
| h::t -> compress_2 t h;;
の作品しかし、なぜこのソリューションが動作しませんか?この場合
let rec compress (l: 'a list) : 'a list =
match l with
| [] -> []
| h::[] -> [h]
| h1::h2::t -> if h1=h2 then h2::(compress t) else h1::h2::(compress t) ;;
あなたのb)解決策について:[1; 2; 2]の結果を考えてみてください。あなたが 'else h1 :: compress(h2 :: t)'のようにh2をリストに残しておけば、うまくいくでしょう。 – lambdapower