Version française
Home     About     Download     Resources     Contact us    
Browse thread
a perplexing difference
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Goffinet <goffinet@c...>
Subject: a perplexing difference

        Hello 

        I'm unable to understand why my fonctions do return these answers 

(* first a vect swapping function*)
let swap k t=
	let prov=t.(k-1) in
	t.(k-1)<-t.(k);t.(k)<-prov;
	t
;;

(*and as I was doubting of everithing I even tried it!*)
let b=swap 3 [|1;2;3;4;5;6|];;
let c=swap 1 b;;
b;;

(*then an iterator which gives a list of the running results : as with the
Mathematica FoldList*)
let rec fold_list f l a=match l with
	[]->[a]
	|p::q->a::fold_list f q (f p a);;

(* first a test with an ordinary function, then another with swap:
        the first one does what I expected it would, 
        the other one yields  four times the same list*) 

let h u v=u+10*v;;
fold_list h [5;6;7] 0;;

fold_list swap [1;3;2] [|10;20;30;40;50|];;

(* so I made another swap_like function, I tried, It works*)
let h x t=[|-x+t.(1);t.(0)|];;
fold_list h [1;3;2] [|10;20;30;40;50|];;

I also tried to Nest my swaps on the command line (with several ((...)))): I
got the  fold_list I expected (and not the constant one)

        As always, more than an answer, a reference to some place in the
caml books where I might have found the answer would be welcomed.

        Thanks!