Sorry my response has been slow.

Let's break your code out as you have started.

Line 1:
Define a recursive function that takes one argument xs

Line 2:
Attempt to resolve the return value for this recursive function based on a match of the argument.

Line 3:
If xs matches an empty list, return a 0 value.

Line 4:
This is the "for all else" case, i.e. for all other xs that are not an empty list, handle the argument with the rest of this line. The "::" is the list head operator, i.e. if I state

"Hello" :: ["World"]

I get a new list

["Hello"; "World"]

So, "y::ys -> y + sumList ys" will result as (in simple terms), "Take the first item from the list" (y::), "Define a function that results in ys" (ys ->), "that is the addition of y (the extracted first value) and recursive accumulation of ys" (y + SumList ys). Note that when I say "recursive" here, I mean "for the next iteration of y" for the evaluation. The recursion will cause this to execute over each item in the list and perform the evaluation. The tail-call here keeps "pushing" down until the list is exhausted and then upon return from each "pop", the function performs the addition until the stack has nothing left to pop.

In this context, F# (and OCaml) run the function until the list is exhausted (left hand side of "::" provides no more instances of y) as no other termination criteria have been specified.

Further references on this can be found here:

Let me know if further information is needed. I'm also trying to describe this in simple terms, so let me know if my language is not concise/precise.

By on 3/23/2006 6:53 AM ()

"Where do you find out what :: means" - a good question. You can't exactly google for '::' I guess we need to place this higher up the quick start material...

The bottom-line answer is of course the source - the following is the definition of lists in prim-types.fs

1
2
3
4
5
6
7
 

type 'a list = 
  | ([])  :                 'a list
  | (::)  : 'a * 'a list -> 'a list

This is the definition of F# lists. The first line says we're defining a type, where the elements carried by the list are of some variable type ('a)

1
2
3
 

type 'a list = 

the start of each of the next two lines says the list is either empty '[]' or a cons '::'

1
2
3
4
 

  | ([])  
  | (::)  

The bits to the right of these says what's involved in being an empty list or a cons node in a list. An empty list carries no data, so it's just a list and nothing else:

1
2
3
 

  | ([])  :                 'a list

A cons cell of a list is constructed from a pair of a head element and a tail list:

1
2
3
 

  | (::)  : 'a * 'a list -> 'a list

So that's what '::' means - it's represents the head and tail of a list.

By on 3/23/2006 4:14 PM ()

Ahhhh I begin to see now. So am I correct in saying:

1
let myList = x::y

is the equivalent of the following in Lisp?:

(setq myList (cons x y) )

I will print out and read that online book that optionsScalper linked to. I'm sure I will have more questions as I read it. Thank you guys for the responses so far.

By on 3/24/2006 9:11 AM ()
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