To expand on the explanations already given, the problem is that

1
   loop |> Seq.cast |> Seq.to_list |> List.rev |> createWalker true walker

is equivalent to:

1
   (((loop |> Seq.cast) |> Seq.to_list) |> List.rev) |> createWalker true walker

But assuming the "if" condition applies, your second attempt would try to lead to code like this:

1
   (loop |> Seq.cast) |> ((Seq.to_list |> List.rev) |> createWalker true walker)

This is not equivalent to your original case (and won't compile because the types work out wrong). As MichaelGG pointed out, the easiest solution is to switch from (|>) to (>>) in the if clause. This is true because (I'm using an equals sign to indicate equivalent code):

1
2
3
4
5
6
  (f |> g) |> h

    =

  f |> (g >> h)  

so:

1
2
3
4
5
6
7
8
9
10
(((loop |> Seq.cast) |> Seq.to_list) |> List.rev) |> createWalker true walker

  =

((loop |> Seq.cast) |> Seq.to_list) |> (List.rev >> createWalker true walker)

  =

(loop |> Seq.cast) |> (Seq.to_list >> (List.rev >> createWalker true walker))
By on 4/3/2009 2:30 PM ()

The left-hand-side of |> must be a value that can be fed into the right-hand-side. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
let twice x = 2*x

4 |> twice |> twice |> printfn "%d"

4 |> twice |> printfn "%d"

// does not compile:
//4 |> twice |>
//    if true then
//        twice |> printfn "%d"
//    else
//        printfn "%d"
        
// ok:
4 |> twice |> (fun x ->
    if true then
        x |> twice |> printfn "%d"
    else
        x |> printfn "%d"        
    )

By on 4/3/2009 2:25 PM ()

In F#, if blocks evaluate to an expression. In the other examples, each if/else has a valid expression. In the one that doesn't work, you have "Seq.to_list |> List.rev |> createWalker true walker". That's not a valid expression for what you want. It's equivalent of writing

1
2
3
4
 

createWalker true walker (List.rev (Seq.to_list ))

You probably want to write:

1
2
3
4
 

Seq.to_list >> List.rev >> createWalker true walker
By on 4/3/2009 2:13 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