-->

dev fsharp

Moving on in the TryFSharp.org site today I finished the last two sections in the Advanced part.

Notes

  • Computational Expressions can be used to “alter the standard evaluation rule of the language and define sub-languages that model certain programming patterns”

    The reason we can alter the semantics of the language is the F# compiler rewrites (de-sugars) computation expressions before compiling.

    For example the code snippet below

      type Age =
      | PossiblyAlive of int
      | NotAlive
    	
      type AgeBuilder() =
          member this.Bind(x, f) =
              match x with
              | PossiblyAlive(x) when x >= 0 && x <= 120 -> f(x)
              | _ -> NotAlive
          member this.Delay(f) = f()
          member this.Return(x) = PossiblyAlive x
    	
      let age = new AgeBuilder()
    	
      let willBeThere a y =
        age { 
          let! current = PossiblyAlive a
          let! future = PossiblyAlive (y + a)
    	
          return future
        }
      willBeThere 38 150
    

    is de-sugared to this:

      let willBeThere2 a y =
        age.Delay(fun () -> 
          age.Bind(PossiblyAlive a, fun current ->
            age.Bind(PossiblyAlive (y+a), fun future ->
              age.Return(future))))
    	
      willBeThere2 38 80
    

    At this point this concept seems too complicated to sink my teeth into. TryFSharp.suggests further study of the following concepts to have a better understanding of computation expressions:

    • The sophisticated architecture of function calls that are generated by de-sugaring
    • Monads - the theory behind computational expressions
  • Quotations is a language feature “that enables you to generate and work with F# code expressions programmatically. This feature lets you generate an abstract syntax tree that represents F# code. The abstract syntax tree can then be traversed and processed according to the needs of your application. For example, you can use the tree to generate F# code or generate code in some other language.”

Conclusion

After dabbling 4 days in TryFSharp.org I think it’s time to move on. There are 4 more sections in the site but since I can only learn by building something on my own I’ll try to come up with a small project to use the basics. Otherwise it’s likely that all this information will be forgotten. It’s already very overwhelming and I need a small achievement to motivate myself.

So I leave TryFSharp.org at this point, for a while at least. Even though I don’t find the advanced topics well-explained it’s still a very nice resource to get started.

TryFSharp.org

Resources

misc tips_and_tricks, jekyll

I’m glad I moved my blog to GitHub pages (especially after I saw this news) and I’m very happy using Jekyll locally while working on my posts. But as a recent Surface Pro 3 owner (review pending) I thought I could work outside home more often which broached the subject how do I synchronize the posts that are not ready to be published.

So far, I was storing them locally on my machine until they are finished which always posed the risk of losing them as they are the sole copy of the post.

Today I discovered a neat feature of Jekyll to end my local copy problem and making all my posts easily synced to everywhere: _drafts folder

Normally the posts go to _posts folder. Now I keep all incomplete posts under _drafts folder. I can checkin anytime as they don’t go live. And I can still use local Jekyll server by using the –drafts flag such as

bundle exec jekyll serve --drafts

With this flag they are compiled like normal posts which allows me to preview them easily.

Best of both worlds!

dev fsharp

It’s hard to accustomed to F# notation after getting used to OO languages like C#. Today I’ll work on the advanced section of the TryFSharp.org site. As I go forward the concepts are taking longer and becoming harder to grasp!

Notes

  • Currying is an interesting concept. It means you don’t have to provide all of the arguments when calling a function. for example

An addition function can be defined as

let add x y = x + y

and another function using this definition can be defined as

let addFive = add 5

In this case addFive returns another function which is waiting for the rest if the arguments. For example the following code snippet prints 17

let add x y = x + y
let addFive = add 5
printfn "%d" (addFive 12)
  • Operators can be overloaded but it should be used with care as it may make the code obfuscated in some cases
type Point(x:float, y:float) =
  member this.X = x
  member this.Y = y

  static member (+) (p1:Point, p2:Point) = 
      new Point(p1.X + p2.X, p1.Y+p2.Y)

let p1, p2 = new Point(0., 1.), new Point(1.,1.)
p1 + p2
  • Active Patterns allow to wrap arbitrary values in a union-like data structure for easy pattern matching.

For example:

let (|Even|Odd|) n =
    if n % 2 = 0 then
        Even
    else
        Odd

In this case Even and Odd are union constructors, so our active pattern either returns an instance of Even or an instance of Odd.

Conclusion

Today I completed the first 2 sections of Advanced section in TryFSharp.org, only 2 more to go. Even though I’m planning to finish all the material in the site it feels like the advanced topics are not covered in depth. The examples provided are too complex for an absolute beginner. I found the F# Programming Wiki as a better resource for these topics and I’m now going over both these resources.

Resources