Cesar

Back to Profile: paks8150

Comments

  • C9 Lectures: Dr. Erik Meijer - Functional Programming ​Fundamental​s, Chapter 2 of 13

    Here are my solutions for #2, #3,#4,#5.

     

    #2

    n = a `div` length xs
          where
             a = 10
             xs = [1 .. 5]

     

    #3
    last' = head . reverse

     

    #4 (Using recursion)

    last'' [x] = x
    last'' (x:y:xs) = last'' (y:xs)


    #5

    init' = reverse . tail . reverse

     

    init'' xs = take (length xs - 1) xs

     

    Nice lecture. I really appreciate all the effort. I think we’re on the verge of a paradigm shift. Functional programming is entering the mainstream, like OOP did in the 90’s. I think learning functional programming makes you a better developer on other languages; in particular C# where some many concepts are taken from FP. Now is the moment to make the effort and learn it.

  • C9 Lectures: Dr. Erik Meijer - Functional Programming ​Fundamental​s, Chapter 1 of 13

    I know the homework was on VB or C#, but this is a lecture on functional programming. So here  is the homework on F# (I tried to make it as close to the Haskell solution as possible):

     

    //Not recomended if the list is big. Lists on F# are not lazy. ys and zs would be computed on the spot.

    //Also, the last statement would blow the stack if the list is too big.

    let rec QS = function
        | [] -> []
        | x::xs ->
            let ys = xs |> List.filter((>=) x)
            let zs = xs |> List.filter((<) x)
            QS ys @ [x] @ QS zs

     

    //Using F# sequences. The equivalent of Haskell lists
    let rec QSLazy = function
        | Empty -> Seq.empty
        | Seq (x,xs) ->
            let ys = xs |> Seq.filter((>=) x)
            let zs = xs |> Seq.filter((<) x)
            Seq.append (QSLazy ys) (Seq.append (seq {yield x}) (QSLazy zs))

     

    //Active Pattern to make pattern matching over F# sequences. Needed to make it look more like the haskell solution
    and (|Empty|Seq|) xs =
        if xs |> Seq.isEmpty then
            Empty
        else
            let x = xs |> Seq.hd
            let xs = xs |> Seq.skip 1 |> Seq.cache
            Seq (x,xs)

     

     

    Nice lecture. I'm looking forward to the next one. Smiley