To answer your question first, the compiler complains because there is a problem inside the for
loop. In F#, let
serves to declare values (that are immutable and cannot be changed later in the program). It isn't a statement as in C# - let
can be only used as part of another expression. For example:
let n = 10
n + n
Actually means that you want the n
symbol to refer to the value 10
in the expression n + n
. The problem with your code is that you're using let
without any expression (probably because you want to use mutable variables):
for x in seq_ do
let list_ = list_ @ [x] // This isn't assignment!
list_
The problematic line is an incomplete expression - using let
in this way isn't allowed, because it doesn't contain any expression (the list_
value will not be accessed from any code). You can use mutable
variable to correct your code:
let mutable list_ = [] // declared as 'mutable'
let seq_ = seq { for x in 1..n do if x % 2 <> 0 then yield List.nth l (x-1)}
for x in seq_ do
list_ <- list_ @ [x] // assignment using '<-'
Now, this should work, but it isn't really functional, because you're using imperative mutation. Moreover, appending elements using @
is really inefficient thing to do in functional languages. So, if you want to make your code functional, you'll probably need to use different approach. Both of the other answers show a great approach, although I prefer the example by Joel, because indexing into a list (in the solution by Chaos) also isn't very functional (there is no pointer arithmetic, so it will be also slower).
Probably the most classical functional solution would be to use the List.fold
function, which aggregates all elements of the list into a single result, walking from the left to the right:
[1;2;3;4;5]
|> List.fold (fun (flag, res) el ->
if flag then (not flag, el::res) else (not flag, res)) (true, [])
|> snd |> List.rev
Here, the state used during the aggregation is a Boolean flag specifying whether to include the next element (during each step, we flip the flag by returning not flag
). The second element is the list aggregated so far (we add element by el::res
only when the flag
is set. After fold
returns, we use snd
to get the second element of the tuple (the aggregated list) and reverse it using List.rev
, because it was collected in the reversed order (this is more efficient than appending to the end using res@[el]
).