tags:

views:

249

answers:

4

What doesx :: xs' mean? I dont have much functional experience but IIRC in F# 1 :: 2 :: 3 :: [];; creates an array of [1,2,3] so what does the ' do?

let rec sum xs =
  match xs with
    | [] -> 0
    | x :: xs' -> x + sum xs'
+4  A: 

The ' is simply part of the variable name. And yes foo :: bar, where foo is an element of type a and bar is a list of type a, means "the list that has foo as its first element, followed by the elements of bar". So the meaning of the match statement is:

If xs is the empty list, the value is 0. If xs is the list containing the item x followed by the items in xs' the value is x + sum xs'. Since x and xs' are fresh variables, this has the effect that for any non empty list, x will be assigned the value of the first element and xs' will be assigned the list containing all other elements.

sepp2k
it sounds like i can write x :: remainder' instead. But what does ' specifically mean? that xs is a list? would x::xs not have xs as the remainder of the list?
acidzombie24
' is just another character that is allowed in names. there is no difference between remainder and remainder' except that the last one is pronounced "remainder prime".
Nathan Sanders
@acidzombie24: The ' means nothing. It's just part of the name. You could as well write `x :: foo`. You can't however write `x :: xs` in your code because the name `xs` is already taken by the parameter to sum. That's all.
sepp2k
The usage of "'" in variables comes from the academic community who like to refer to variables as x, x' (pronounced x-prime), f(x), f'(x), etc.
Juliet
@sepp2k Well, you *can* write the pattern `x :: xs`, it's just that you will no longer be able to access the original `xs` inside that branch.
Pascal Cuoq
+7  A: 

I think sepp2k already answered most of the question, but I'd like to add a couple of points that may clarify how F#/OCaml compiler interprets the code and explain some common uses.

Regarding the ' symbol - this is just a part of a name (a valid identifier starts with a letter and then contains one or more letters, numbers or ' symbols). It is usually used if you have a function or value that is very similar to some other, but is in some way new or modified.

  • In your example, xs is a list that should be summed and the pattern matching decomposes the list and gives you a new list (without the first element) that you need to sum, so it is called xs'

  • Another frequent use is when declaring a local utility function that implements the functionality and takes an additional parameter (typically, when writing tail-recursive code):

    let sum list =
      let rec sum' list res = 
        match list with
        | [] -> res
        | x::xs -> sum' xs (res + x)
      sum' list 0
    

However, I think there is usually a better name for the function/value, so I try to avoid using ' when writing code (I think it isn't particularly readable and moreover, it doesn't colorize correctly on StackOverflow!)

Regarding the :: symbol - as already mentioned, it is used to create lists from a single element and a list (1::[2;3] creates a list [1;2;3]). It is however worth noting that the symbol can be used in two different ways and it is also interpreted in two different ways by the compiler.

When creating a list, you use it as an operator that constructs a list (just like when you use + to add two numbers). However, when you use it in the match construct, it is used as a pattern, which is a different syntactic category - the pattern is used to decompose the list into an element and the remainder and it succeeds for any non-empty list:

// operator
let x = 0
let xs = [1;2;3]
let list = x::xs

// pattern
match list with
| y::ys -> // ...
Tomas Petricek
The :: is called the cons operator, if I'm not mistaken.
TwentyMiles
You forgot to declare `sum'` function recursive. Also, there should be a `in` keyword after the match.
Mikael S
Thanks - I added the `rec` keyword. I wrote the code in F#, which is whitespace-sensitive and doesn't require `in` keywords.
Tomas Petricek
+1  A: 

It's idiomatic in ML-family languages to name a variable foo' to indicate that it's somewhat related to another variable foo, especially in recursions like your code sample. Just like in imperative languages you use i, j for loop indices. This naming convention may be a little surprising since ' is typically an illegal symbol for identifiers in C-like languages.

Wei Hu
+1  A: 

Like others have said, the ' is a carryover from mathematics where x' would be said as "x prime"

Nick Canzoneri