# Abs function is not working with abs -10

**URL:** <https://discuss.ocaml.org/t/abs-function-is-not-working-with-abs-10/904>\
**Category:** Learning\
**Created:** [October 2, 2017, 11:11am UTC](https://discuss.ocaml.org/t/abs-function-is-not-working-with-abs-10/904 "2017-10-02T11:11:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ranamustafa](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@ranamustafa](https://discuss.ocaml.org/u/ranamustafa)\
**Post date:** [October 2, 2017, 11:11am UTC](https://discuss.ocaml.org/t/abs-function-is-not-working-with-abs-10/904/1 "2017-10-02T11:11:14Z")

</div>

hi  
i want to learn ocaml and i started since days. i have this problem. why the function abs -10 is not working  
but when write abs (-10) its work.

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [October 2, 2017, 12:35pm UTC](https://discuss.ocaml.org/t/abs-function-is-not-working-with-abs-10/904/2 "2017-10-02T12:35:00Z")

</div>

This is one of the particularities of OCaml lexical convention: the compiler reads `abs -10` as `(abs) - (10)`, and not as `(abs) (-10)`. Consequently, it then raises an error due to the fact that `abs` is a function of type `int->int` whereas the binary operator `-` expected an `int` as its first argument.

If you want to use the unary operator, they are two possibility: either uses parenthesis `abs (-10)` or the (other) unary minus operator `~-`: `abs ~-10` .

---

<div class="post-metadata">

**Author:** ![ranamustafa](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@ranamustafa](https://discuss.ocaml.org/u/ranamustafa)\
**Post date:** [October 2, 2017, 12:55pm UTC](https://discuss.ocaml.org/t/abs-function-is-not-working-with-abs-10/904/3 "2017-10-02T12:55:59Z")

</div>

but how we can know that the compiler will read the function as abs -10 or  
(abs) (-10) ?. I mean for other operation also.

---

<div class="post-metadata">

**Author:** ![ivg](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ivg/32/103_2.png) [@ivg](https://discuss.ocaml.org/u/ivg)\
**Post date:** [October 2, 2017, 1:08pm UTC](https://discuss.ocaml.org/t/abs-function-is-not-working-with-abs-10/904/4 "2017-10-02T13:08:58Z")

</div>

> [@ranamustafa](#):
>
> but how we can know that the compiler will read the function as abs -10 or
> 
> (abs) (-10) ?. I mean for other operation also.

It is defined by [precedences of operators](http://caml.inria.fr/pub/docs/manual-ocaml/expr.html). The idea is that some operators binds tighter than another, the same as with `a + b * c`, from the school we know, that it should be read as `a + (b * c)` because the `*` operator has a higher precedence than `+`. The function application operator, that is written just as a juxtaposition in OCaml, i.e., `f x`, has higher precedence then any infix, i.e., it binds tigher than operators starting with some non-letter symbol, e.g., `+,-,*,/,...` It , however, has a precedence that is lower than of the prefix operators, e.g., `~,!`. So you can write `abs ~-10` where `~-` is the prefix unary minus operator. (Note that a single `-` is the binary minus operator, even if you put it close to a number it will not be treated as a part of the number, neither it will be treated as an unary operator).
