let iota n = List.init n ((+)1) in let sum lst= List.fold_left (+) 0 lst in let rec take n lst = match n with 0 -> [] | 1 -> [List.hd lst] | _ -> (List.hd lst)::take (n-1) (List.tl lst) in let sigma lst = List.rev (List.fold_left (fun x y->sum (take y lst)::x) [] (iota (List.length lst))) in sigma [1;2;3;4;5;6];;
ありがとう。こんな感じですかね (途中まで同じ) let sigma lst = let accum = ref [] in List.rev (List.fold_left (fun x y-> accum:=y::!accum; sum !accum::x) [] (iota (List.length lst))) in sigma [1;2;3;4;5;6];;
let sigma lst = let rec sum lst = match lst with | [] -> [] | car :: cdr -> (List.fold_left (+) 0 lst) :: (sum cdr) in List.rev (sum (List.rev lst));; こんなんどうでっしゃろ?
let scanl1 f = function | [] -> [] | (x::xs) -> scanl f x xs let scanl f x y = List.rev (List.fold_left (fun a b -> (f (List.hd a) b) :: a) [x] y) で scanl1 (+) [1;2;3;4];; ってできるし。 HaskellとOCamlの差が出てくるのはやっぱりIOとかかね。
>>454亀だがちょうど最近覚えたので確認のために fun swap (date,i,j)= let val a = Array.sub(date,i) val b = Array.sub(date,j) in Array.update(date,i,b); Array.update(date,j,a) end localでArrayをopenしておいたほうが綺麗かも
val emptyset : 'a Set val elem : 'a Set -> 'a * 'a Set val member : ''a -> ''a Set -> bool val add : ''a -> ''a Set -> ''a Set val map : ('a -> 'b) -> 'a Set -> 'b Set val allelem : ''a Set -> (''a * ''a Set) Set val fromList : 'a list -> 'a Set val toList : 'a Set -> 'a list
(* Compute a pair of the element of input lists and rest set *) fun elem [] = raise EmptySet | elem (x::xs) = (x,xs);
(* Check whether x is an element of a set *) fun member _ [] = false | member x (y::ys) = (x = y orelse member x ys);
526 名前:522 mailto:sage [2008/08/24(日) 15:05:15 ]
(続き)
(* Add an element to a set *) fun add a A = if member a A then A else a::A;
val map = List.map;
(* Compute {(a,A \ {a}) | a \in A} *) (* where A is an input set *) fun allelem A = let val (b,B) = elem A in add (b,B) (map (fn (c,C) => (c, add b C)) (allelem B)) end handle EmptySet => emptyset;
エラーは以下のとおりです. Diagnoses: stdIn:12.1-12.49 Diagnosis(typecheckExp 12): expression type and annotation don't agree expression type: (int * int Set.Set) Set.Set -> (int * int Set.Set) list annotation: (int * int Set.Set) Set.Set -> (int * int Set.Set) list stdIn:12.1-12.49 Diagnosis(typecheckExp 16): opetator and operand don't agree operator type: (int * int Set.Set) Set.Set -> (int * int Set.Set) list operand types: (int * int Set.Set) Set.Set stdIn:12.1-12.51 Diagnosis(typecheckExp 12): expression type and annotation don't agree expression type: (int * int Set.Set -> expression) -> (int * int Set.Set) list -> expression annotation: (int * int Set.Set -> expression) -> (int * int Set.Set) list -> expression stdIn:12.1-12.51 Diagnosis(typecheckExp 16): expression type and annotation don't agree expression type: int * int Set.Set annotation: int * int Set.Set stdIn:12.1-12.51 Diagnosis(typecheckExp 12): expression type and annotation don't agree expression type: int Set.Set -> expression annotation: int Set.Set -> expression
528 名前:522 mailto:sage [2008/08/24(日) 15:11:46 ]
(エラー続き)
stdIn:12.1-12.51 Diagnosis(typecheckExp 16): expression type and annotation don't agree expression type: int * int Set.Set annotation: int * int Set.Set stdIn:12.1-12.51 Diagnosis(typecheckExp 16): opetator and operand don't agree operator type: int Set.Set -> expression operand types: int Set.Set stdIn:12.1-12.51 Diagnosis(typecheckExp 16): opetator and operand don't agree operator type: (int * int Set.Set -> expression) -> (int * int Set.Set) list -> expression operand types: int * int Set.Set -> expression
529 名前:522 mailto:sage [2008/08/24(日) 15:13:41 ]
(さらに続き)
stdIn:12.1-12.51 Diagnosis(typecheckExp 16): opetator and operand don't agree operator type: (int * int Set.Set) list -> expression operand types: (int * int Set.Set) list stdIn:12.1-12.51 Diagnosis(typecheckExp 12): expression type and annotation don't agree expression type: (int * int Set.Set) list -> expression annotation: (int * int Set.Set) list -> expression stdIn:12.1-12.51 Diagnosis(typecheckExp 16): opetator and operand don't agree operator type: (int * int Set.Set) list -> expression operand types: (int * int Set.Set) list BUG :invalid type