Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluation d'une expression [item1]@liste@[item2] dans une fonction récursive #7510

Closed
vicuna opened this issue Mar 25, 2017 · 1 comment
Closed
Assignees
Labels

Comments

@vicuna
Copy link

vicuna commented Mar 25, 2017

Original bug ID: 7510
Reporter: LucFreget
Assigned to: @Octachron
Status: resolved (set by @Octachron on 2017-03-25T17:45:13Z)
Resolution: not a bug
Priority: normal
Severity: trivial
Platform: Mac
OS: MacOS
OS Version: Sierra 10.12.3
Version: 4.04.0
Category: misc

Bug description

Dans une fonction (en l'occurrence, une fonction récursive), l'expression [h1]@L2@[h2] n'est pas évaluée de la même façon que (xxx h1 h2 l2) quand xxx est définie comme suit:

let xxx a b l = [a]@l@[b];;

Steps to reproduce

#let la = [0; 0; 0; 0];;
#let lb = [1; 2; 3; 4];;

#let print_list l = List.map (fun item -> print_int item) l;;
#print_list la;;
0000
#print_list lb;;
1234

let xxx a b l = [a]@l@[b];;

let rec fusion l1 l2 =

begin print_list l1; print_string " "; print_list l2; print_string "\n";
match l1 with | [] -> l2 | [h] -> [h]@L2 | h1::h2::t -> fusion t (xxx h1 h2 l2); end;;

fusion lb la;;

1234 0000
34 100002
31000024

  • : int list = [3; 1; 0; 0; 0; 0; 2; 4]

JUSQU'ICI, TOUT VA BIEN

let rec fusion2 l1 l2 =

begin print_list l1; print_string " "; print_list l2; print_string "\n";
match l1 with | [] -> l2 | [h] -> [h]@L2 | h1::h2::t -> fusion2 t [h1]@L2@[h2]; end;;

fusion2 lb la;;

1234 0000
34 1
314

  • : int list = [3; 1; 4; 0; 0; 0; 0; 2]

La valeur retournée par la fonction fusion2 est différente de celle retournée par fusion, alors que la seule différence entre les deux fonctions est le remplacement de (xxx h1 h2 l2) par [h1]@L2@[h2].

Lorsque fusion2 est appelée récursivement, son deuxième argument vaut [1], quand on s'attend à [1; 0; 0; 0; 0; 2].

Additional information

Autre bizarrerie: dans l'exemple suivant, une arobase a été oubliée dans [h1]@L2[h2].

let rec fusion l1 l2 =

begin print_list l1; print_string " "; print_list l2; print_string "\n";
match l1 with | [] -> l2 | [h] -> [h]@L2 | h1::h2::t -> fusion t [h1]@L2[h2]; end;;
Warning 10: this expression should have type unit.
Warning 10: this expression should have type unit.
Error: This expression has type int list
This is not a function; it cannot be applied.

fusion lb la;;

1234 0000
34 100002
31000024

  • : int list = [3; 1; 0; 0; 0; 0; 2; 4]
@vicuna
Copy link
Author

vicuna commented Mar 25, 2017

Comment author: @Octachron

There is no issue within the code here: function application has a higher precedence than the concatenation operator (@). See http://ocaml.org/learn/ for more information.

Consequently,

 fusion2 t [h1]@l2@[h2] = (fusion2 t [h1])  @l2@[h2] 

<> fusion2 t ([h1]@L2@[h2]) = fusion2 t (xxx h1 h2 l2)

Il n'y aucun problème avec le code ici: l'application d'une fonction a simplement une plus grande priorité que l'opérateur de concaténation. Une explication plus approfondie du language OCaml est disponible sur http://ocaml.org/learn/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants