Cesar

Cesar paks8150

Comments

  • C9 Lectures: Dr. Erik Meijer - Functional Programming Fundamentals Chapter 6 of 13

    More homework:

    1) Prove that fac n = recfac n

     

    Definition of recfact n:

    (1)        recfac 0 = 1

    (2)        recfac (n+1) = (n+1) * recfact n

     

    Definition of fac n:

    (3)        fac n = product [1...n]

                            where

    (4)                                product [] = 1

    (5)                                product [1..n] = 1 * 2 * … (n-1) * n

     

    We want to prove that:

    Devil                    fact n = recfac n

     

    Using induction:

     

    Case n = 0

                fac 0 = recfac 0

     

                fac 0 = 1 (Eq. 1)

     

                recfac 0 = product [1 .. 0] = product [] = 1 (Eq. 4)

     

                1 = 1

     

    Case (m + 1):

    (7)        fac (m+1) = recfac (m+1)

     

    fac (m+1) = product [  1..(m+1)] = 1 * 2 *….* m * (m+1)

     

    1. Using Eq.5:       1 * 2 *….* m  = product [1..m]

     

                fac (m+1) = 1 * 2 *….* m * (m+1)  =(product [1..m]) * (m+1)

     

    (8)        fac (m+1) = (product [1..m]) * (m+1)

     

    Using Eq 2 :  recfac (m+1) = (m+1) * recfac m

     

     

    Replacing Eq 8 into Eq 7:

                fac (m+1) = (product [1..m]) * (m+1) = recfac (m+1)

     

    Using Eq 2 on the right side:

                (product [1..m]) * (m+1) = (m+1) * recfac m

     

    Dividing by (m+1) on both sides:

                product [1..m] = recfac m

     

    Using Eq 3 on the left side:

                fac m = recfac m

     

    Q.E.D.! Smiley

  • C9 Lectures: Dr. Erik Meijer - Functional Programming Fundamentals Chapter 6 of 13

    Again, nice lecture.  I'm lloking forward to the rest.

     

    Here is my take on Append:

    public static IEnumerable<T> Append<T>(this IEnumerable<T> a, IEnumerable<T> b) { foreach (var t in a) { yield return t; } foreach (var t in b) { yield return t; } } 

  • C9 Lectures: Dr. Erik Meijer - Functional Programming Fundamentals Chapter 5 of 13

    Here is my take on the homework:

     

    1. Without looking at the definitions from the standard prelude, define the  following library functions using recursion.

     

    - Decide if all logical values in a list are True:

    and' :: [Bool] -> Bool and' [] = True and' (False:_) = False and' (_:xs) = and xs

     

    - Concatenate a list of lists:

    concat' :: [[a]] -> [a] concat' [] = [] concat' (xs:xss) = xs ++ concat' xss

     

    - Produce a list with n identical elements:

    replicate' :: Int -> a -> [a] replicate' 0 x = [] replicate' (n+1) x = x:replicate' n x

     

    - Select the nth element of a list

    (!!@) :: [a] -> Int -> a (!!@) (x:_) 0 = x (!!@) (_:xs) (n+1) = xs !!@ n

     

    - Decide if a value is an element of a list:

    elem' :: (Eq a) => a -> [a] -> Bool elem' _ [] = False elem' x (y:ys) | x == y = True | otherwise = elem' x ys

     

    2. Define a recursive function merge:: Ord a => [a] -> [a] -> [a] that  merges two sorted lists to give a single sorted list.

    merge:: (Ord a) => [a] -> [a] -> [a] merge [] [] = [] merge xs [] = xs merge [] ys = ys merge (x:xs) (y:ys) | x < y = x:merge xs (y:ys) | x == y = x:y:merge xs ys | otherwise = y:merge (x:xs) ys

     

    3. Define a recursive function msort :: Ord a => [a] -> [a] that  implements merge sort

    msort :: (Ord a) => [a] -> [a] msort [] = [] msort [x] = [x] msort xs = merge (msort ys) (msort zs) where [(ys,zs)] = halve xs halve :: [a] -> [([a],[a])] halve [] = [] halve xs = [(take n xs, drop n xs)] where n = (length xs `div` 2)

     

  • C9 Lectures: Dr. Erik Meijer - Functional Programming Fundamentals Chapter 4 of 13

    Here is my take on part of the homework

     

    1. safetail

    safetaila :: [a] -[a] safetaila xs = if null xs then [] else tail xs safetailb :: [a] -[a] safetailb xs | null xs = [] | otherwise = tail xs safetailc :: [a] -[a] safetailc [] = [] safetailc (_:xs) = xs 


    2. Implement (||)  in three different ways

    (||!) :: Bool -Bool -Bool True ||! True = True True ||! False = True False ||! True = True False ||! False = False (||@) :: Bool -Bool -Bool False ||@ False = False _ ||@ _ = True (||#) :: Bool -Bool -Bool False ||# b = b True ||# _ = True


    3. Redefine (&&)

    (&&$) a b = if a == True && b == True then True else False


    4. Redefine (&&) again

    (&&%) a b = if a == True then b else False

  • C9 Lectures: Dr. Erik Meijer - Functional Programming Fundamentals Chapter 5 of 13

    At last Thursday is here. Smiley

     

    Excelent presentation as always. The only thing is that you missed an excelent opportunity to implement "where" using a list comprehension.

     

    where' :: [a] -> (Int -> a -> Bool) -> [a] where' xs p = [x | (x,i) <- xs', p i x ] where xs' = zip xs [0..]

     

    Thanks again.

    I'm looking forward to the next lecture.

  • C9 Lectures: Dr. Erik Meijer - Functional Programming Fundamentals Chapter 4 of 13

    To get what  you want you can use the function flip that is on the haskell prelude.

     

    The Haskell  prelude defines flip as:

    -- flip f  takes its (first) two arguments in the reverse order of f.
    flip             :: (a -> b -> c) -> b -> a -> c
    flip f x y       =  f y x
    

     

    using flip we can write (-1) 3 as:

     

    (flip (-) 1) 3  

     

    Also, the prelude has a substract function defined as:

     

    subtract         :: (Num a) => a -> a -> a
    subtract         =  flip (-)
    

     

  • C9 Lectures: Dr. Erik Meijer - Functional Programming Fundamentals Chapter 4 of 13

    I love the “back to school” feeling I get with these lectures. I like Novox have been waiting all day for this. Keep up the good work Smiley

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

    Here is part of the homework:

     

    {-
    1.- What type are the following values:
        ['a','b','c'] :: [Char]
        ('a','b','c') :: (Char,Char,Char)
        [(False,'0'),(True,'1')] :: [(Bool,Char)]
        ([False, True],['0','1']) :: ([Bool],[Char])
        [tail, init, reverse] :: [[a] -> [a]]
    -}
    
    -- 2.- What are the type of the following functions:
    
    second :: [a] -> a
    second xs = head (tail xs)
    
    swap :: (a,b) -> (b,a)
    swap (x,y) = (y,x)
    
    pair:: a -> b -> (a,b)
    pair x y = (x,y)
    
    double :: (Num a) => a -> a
    double x = x*2
    
    palindrome :: (Eq a) => [a] -> Bool
    palindrome xs = reverse xs == xs
    
    twice :: (a -> a) -> a -> a
    twice f x = f (f x)
    

     

    Nice lecture, please keep up the good work!!! Smiley

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

    I think you're missing a great opportunity here. Please ask questions about all the thing that Erik is presenting and you don't understand. I bet you would get a lot of help in the forum. That would help a lot of people that might be having the same issues as you. Also it would give Erik an inside of what concepts are difficult to grasp and help him to better present future chapters.

     

    Another think hat you can do is read the chapter of the book that Erick is going to present in advance. That way you know what's going to be covered. If you don't have the book, the slides are here: http://www.cs.nott.ac.uk/~gmh/book.html#slides.

     

    I hope this helps. Smiley

     

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

    Exelent! I was looking forward to the next lecture. Smiley

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

    Thanks exoteric. My original solution for init' didn't use composition until I saw the lecture and Erik started talking about function composition (yes, I did the exercises from the book before the lecture Tongue Out ). I'm not sure if good at this point of the lecture to introduce pointfree style. But how am I to say Smiley

    This was my original solution:

     

    init’ xs = reverse (tail (reverse xs))

     

    Which I think people not familiar with Haskell would understand better than

     

     

    init’ = reverse . tail . reverse

     

    I say this, because it took me a while to understand what was going on with the second definition and why the parameter was implicit.

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

    I think you're being too harsh on F#. F# is an excellent bridge for people that want to learn functional programming. I has all the familiar things that an imperative programmer expects like mutable variables, the “for” loop, objects, Visual Studio support. But once you let go the training wheels, you have access to all the benefits of a functional language.