# Need help please about some exercices

**URL:** <https://discuss.ocaml.org/t/need-help-please-about-some-exercices/10227>\
**Category:** Learning\
**Created:** [July 26, 2022, 8:55pm UTC](https://discuss.ocaml.org/t/need-help-please-about-some-exercices/10227 "2022-07-26T20:55:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![catie](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@catie](https://discuss.ocaml.org/u/catie)\
**Post date:** [July 26, 2022, 8:55pm UTC](https://discuss.ocaml.org/t/need-help-please-about-some-exercices/10227/1 "2022-07-26T20:55:10Z")

</div>

hi e erybody  
i have a list [“first” ; “second” ; “three”]

i want to return this list without the last element like that :retourne [“first” ; “second”]\*)

I try this but something doesn’t function:

```auto
let rec remove_last l =
  let lgth = List_length l in
  let ret = [] in
  if lgth < 2 then ret
  else List.hd l :: remove_last (List.tl l);;

print_string (remove_last l ["first" ; "second" ; "last"]);;

```

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [July 26, 2022, 9:22pm UTC](https://discuss.ocaml.org/t/need-help-please-about-some-exercices/10227/2 "2022-07-26T21:22:19Z")

</div>

Please tell us what doesn’t work and the error messages you see, it makes it easier for us to help you.

In this case I suspect you need to replace `List_length` by `List.length` and your code should be right.

However note that calling `List.length l` on each recursive call needs to go through the whole `l` which is inefficient (see the [implementation](https://github.com/ocaml/ocaml/blob/d8887f9e2fd613107ebd1b27c4974483eb22b214/stdlib/list.ml#L21-L25) of `List.length`).

This is a case where you should use pattern matching. Here’s the structure, I’ll let you fill in the blanks:

```auto
let rec remove_last l = match l with
| [] -> …
| [v] -> …
| v :: vs -> …

```

---

<div class="post-metadata">

**Author:** ![catie](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@catie](https://discuss.ocaml.org/u/catie)\
**Post date:** [July 26, 2022, 9:28pm UTC](https://discuss.ocaml.org/t/need-help-please-about-some-exercices/10227/3 "2022-07-26T21:28:37Z")

</div>

> [@dbuenzli](#):
>
> List.length

i have this error "Error: This function has type 'a list → ‘a list  
It is applied to too many arguments; maybe you forgot a `;’.

by the way I prefer to avoid the function match with

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [July 26, 2022, 9:31pm UTC](https://discuss.ocaml.org/t/need-help-please-about-some-exercices/10227/4 "2022-07-26T21:31:39Z")

</div>

Ah it’s here:

> [@catie](#):
>
> `print_string (remove_last l ["first" ; "second" ; "last"]);;`

you need to remove that `l` :–)

---

<div class="post-metadata">

**Author:** ![catie](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@catie](https://discuss.ocaml.org/u/catie)\
**Post date:** [July 26, 2022, 9:49pm UTC](https://discuss.ocaml.org/t/need-help-please-about-some-exercices/10227/5 "2022-07-26T21:49:03Z")

</div>

you say I have to cancel all this line or only the ‘l’  
sorry I m beginner and I don’t understand any about this language

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [July 27, 2022, 6:30am UTC](https://discuss.ocaml.org/t/need-help-please-about-some-exercices/10227/6 "2022-07-27T06:30:08Z")

</div>

> [@catie](#):
>
> I try this but something doesn’t function:

The way to work with lists is to use pattern matching:

```auto
let rec remove_last = function
  | [] | [_] -> []
  | x :: xs -> x :: remove_last xs

```

If you don’t understand anything about the language, then it may be a good idea to start by reading about the basics, I recommend [OCaml from the Very Beginning](https://johnwhitington.net/ocamlfromtheverybeginning/).

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![fccm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/fccm/32/1587_2.png) [@fccm](https://discuss.ocaml.org/u/fccm)\
**Post date:** [July 27, 2022, 12:58pm UTC](https://discuss.ocaml.org/t/need-help-please-about-some-exercices/10227/7 "2022-07-27T12:58:14Z")

</div>

[![](https://toutpourlejeu.com/17213-home_default/puzzle-en-bois-classement-formes-couleurs-et-hauteurs.jpg) ](https://toutpourlejeu.com/17213-home_default/puzzle-en-bois-classement-formes-couleurs-et-hauteurs.jpg)  
The ocaml type system is like this toy for children, you can not put a triangle in the square shape.

Here `remove_last` returns a `string list`, but `print_string` expects a `string`.

So here you can use `List.iter` to apply `print_string` on every elements of the list, or you can also use `String.concat` to concatenate every string of the list into a single string:

```auto
$ ocaml
# let rec remove_last = function
  | [] | [_] -> []
  | x :: xs -> x :: remove_last xs
  ;;
val remove_last : 'a list -> 'a list = <fun>

# print_string (remove_last ["first" ; "second" ; "last"]) ;;
Error: This expression has type string list
       but an expression was expected of type string

# List.iter print_string (remove_last ["first" ; "second" ; "last"]) ;;
firstsecond
- : unit = ()

# print_string (
    String.concat " " (remove_last ["first"; "second"; "last"])) ;;
first second
- : unit = ()

```

(copy-pasted from the toplevel)

```auto
$ ocaml
# List.iter ;;
- : ('a -> unit) -> 'a list -> unit = <fun>

# String.concat ;;
- : string -> string list -> string = <fun>

```

---

<div class="post-metadata">

**Author:** ![catie](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@catie](https://discuss.ocaml.org/u/catie)\
**Post date:** [September 12, 2022, 1:01pm UTC](https://discuss.ocaml.org/t/need-help-please-about-some-exercices/10227/8 "2022-09-12T13:01:50Z")

</div>

please i would like to returns [first,second] and. not first second

---

<div class="post-metadata">

**Author:** ![fccm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/fccm/32/1587_2.png) [@fccm](https://discuss.ocaml.org/u/fccm)\
**Post date:** [September 13, 2022, 11:37pm UTC](https://discuss.ocaml.org/t/need-help-please-about-some-exercices/10227/9 "2022-09-13T23:37:11Z")

</div>

Please tell us why you’re not able to figure out how to change the example, to fit what you want.

The answer is quite simple, just replace the space `" "` by a comma `","` and use the concatanation operator `^`:

```auto
# let s = String.concat "," (remove_last ["first"; "second"; "last"]) ;;
val s : string = "first,second"

# print_endline ("[" ^ s ^ "]") ;;
[first,second]
- : unit = ()

```
