Hi,

I don't have an F# compiler where I'm now, but as far as I can see:

1) RunningSumList should be a bigint, not a list. sum_tr just calculates the sum of the elements in the list. It does not give you a running sum, that you seem to be expecting.

2) This means that you can't call Length on it, or index it.

3) IncrementedSummedList is not declared anywhere, and in that line '=' means compare, not assign.

Try commenting out the for loop, and hover over RunningSumList. It should tell you it's of type bigint. Work from there.

hope this helps,

Kurt

By on 11/14/2008 12:59 AM ()

Thanks Kurt, you're right. Now I found out how to get it working. I changed the recursion so accu is a list.

This code has the annoying feature that the list is reversed from original order: as I pull heads off the original list and stick heads onto the output list, the order of elements is reversed. I would prefer the output list to be [0; 1; 3; 6; 10; 15] .

To reverse a list, is the most efficient way to convert it to a string, reverse the string, and convert the string to a list? That would be ugly.

let rec sum_tr_aux (accu) = function

| [] -> accu

| h::t -> sum_tr_aux ( (h + (List.hd accu)) :: accu) t

let sum_tr list =

sum_tr_aux [0] list

let SummandList = [1; 2; 3; 4; 5]

let RunningSumList = sum_tr SummandList

print_any RunningSumList;;

val sum_tr_aux : int list -> int list -> int list

val sum_tr : int list -> int list

val SummandList : int list

val RunningSumList : int list

[15; 10; 6; 3; 1; 0]>

David

By on 11/17/2008 9:36 AM ()

To reverse a list, just call List.rev.

[link:research.microsoft.com]

It is common to write recursive list algorithms which generate the result in reverse order and then call "rev" as the last step.

By on 11/17/2008 12:37 PM ()

Now I have that working, but I have a new problem. I want to make the recursion more complex by passing in another list and doing something with it. To do so, I defined a class containing two lists, and I want to access the object within the recursion, see the comments in the code below. But I get a complier error: "The type 'ListObject' is not defined"

How can I pass a user-defined object into a recursion and access its elements within the recursion? Thanks!

let List1 = [0.1; 0.2; 0.3; 0.4; 0.5]

let List2 = [1; 2; 3; 4; 5]

type ListPair =

val private _List1 : float list

val private _List2 : int list

new (List1, List2) =

{_List1 = List1; _List2 = List2}

member v.List1 = v._List1

member v.List2 = v._List2

let ListObject = new ListPair(List1, List2);

// I want to pass both lists List1 and List2 into the recursion so I pass the ListObject

let rec sum_tr_aux accu list_pair:ListPair =

let list_1 = list_pair.List1 // compiler says list_pair is of "indeterminate type"

let list_2 = list_pair.List2 // I want to use this also in the recursion; code to be added later

match list_1 with

| [] -> accu

| h::t -> sum_tr_aux ( (h + (List.hd accu)) :: accu ) t // compiler thinks something here is of type 'ListPair' but that is not what I intend. There should be only ints and int lists.

let sum_tr list =

sum_tr_aux [0] list

//let SummandList = [1; 2; 3; 4; 5]

let SummandList = ListObject.List2 // I don't want to have to break it out here.

let RunningSumList_rev = sum_tr SummandList

let RunningSumList = List.tl (List.rev RunningSumList_rev) // reverse the list and omit the first item

print_any RunningSumList

By on 11/18/2008 10:52 AM ()

I am unclear of all the details, but your first stumbling point is

let rec sum_tr_aux accu list_pair:ListPair =

This declares the return type to be a ListPair. You really want this instead:

let rec sum_tr_aux accu (list_pair:ListPair) =

By on 11/18/2008 2:51 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper