Tips on staff management - Issue #01

by Greg 23. September 2010 13:37

From a software management type who shall remain nameless:

then punch him in the right nut and laugh while he cries on the floor

Tags:

Management

Programming in Haskell - Chapter 2

by Greg 20. September 2010 12:26

I found time last night to read the second chapter of Programming in Haskell. Here are my results for the exercises:

Exercise 1 - Parenthesise the following arithmetic expressions.

Part 1:

 2 ^ 3 * 4
-- becomes
 ( 2 ^ 3 ) * 4

Correct

Part 2:

 2 * 3 + 4 * 5
-- becomes
 ( 2 * 3 ) * ( 4 * 5 )

Correct

Part 3:

 2 + 3 * 4 ^ 5
-- becomes
 2 + ( 3 * ( 4 ^ 5 ) )

Correct

Exercise 2 - No solution required.

Exercise 3 - The script below contains three syntactic errors. Correct these errors and check your script works properly using Hugs.


 N = a 'div' length xs
     where
      a = 10
     xs = [1, 2, 3, 4, 5]

-- becomes

 n = a ‘div‘ length xs
     where
       a = 10
       xs = [1, 2, 3, 4, 5]

The issues are:

  1. The function name N is upper case and should be lower case.
  2. The function call 'div' is enclosed in the wrong type of quotation marks.
  3. The local definitions for a and xs are incorrectly indented. 

Correct

Exercise 4 - Show how the library function last that selects the last element of a non-empty list can be defined using the functions introduced in this chapter.

Part 1:

-- first definition of last
last xs = head reverse xs

Incorrect

I get a compile error in Hugs as I did not parenthesize the call to reverse xs. The code should have looked like this:

last xs = head ( reverse xs )

Part 2:

-- second definition of last
last xs = xs !! n
          where 
            n = ( length xs ) -1

The example given is more concise than my version but is fundamentally the same:

last xs = xs !! (length xs − 1)

Correct

Exercise 5 - Show how the library function init that removes the last element from a list can be defined in two different ways.

Part 1:

-- first definition of init
init xs = take n xs
          where
            n = ( length xs ) - 1          

Again, the solution defines the number to take without using a local definition but the formula is essentially the same, and I think I like the where syntax better than in-lining everything.

Correct

Part 2:

-- second definition of init
init xs = reverse tail reverse xs

Incorrect

I've made the same mistake here that I made in exercise 4. I haven't correctly parenthesized my function calls and as such I get Type error:

ERROR file:{Hugs}\packages\hugsbase\Hugs.hs:13 - Type error in application
*** Expression     : reverse tail reverse xs
*** Term           : reverse
*** Type           : [e] -> [e]
*** Does not match : a -> b -> c -> d

 

Conclusion

That makes me 8 out of a possible 10 for chapter 2. I can't say I'm surprised as I only just checked my working in Hugs as I was writing this post. I didn't have the laptop turned on last night so I didn't actually do Exercise 2. If I had, I might have learned the parenthesization of function calls before going on to complete the other exercises. This will be a lesson to myself.

Tags: ,

Haskell

Enabling CodeFormatterExtension on BlogEngine.NET

by Greg 19. September 2010 19:18

I just want to give a quick link to this guy who has a rather obvious fix for getting CodeFormatterExtension working on BlogEngine.

Tags:

BlogEngine.NET

Programming in Haskell - Chapter 1

by Greg 19. September 2010 17:22

I just read the first chapter of Programming in Haskell by Graham Hutton, afterwhich I watched the accompanying lecture by Erik Meijer on Channel 9. I then did the exercises from the end of the chapter. Here are my results.

Exercise 1 - Give another possible calculation for the result of double ( double 2 ).


    double ( double 2 )  
 =     { applying the outer double }
    ( double 2 ) + ( double 2 )
 =     { applying second double }
    double 2 + ( 2 + 2 )
 =     { applying the first double }
    2 + 2 + 2 + 2
 =     { applying plus }
    8

This isn't in the official solutions document but I can't see how it's invalid. If I find out otherwise I'll update this post. For now I'm giving myself a mark for this exercise.

Correct

Exercise 2 - Show that sum [x] = x for any number x.


    sum [ 3 ]
 =    { applying sum }
    3 + ( sum [] )
 =    { applying sum }
    3 + 0
 =    { applying plus }
    3

I marked myself as correct for this as the working out is right however I did make a note to myself that, when proving something for x, the working out should use x. In my working out I have an assumed a that x is 3.

Correct

Exercise 3 - Define a function product that produces the product of a list of numbers, and show using your definition that product [ 2. 3. 4 ] = 24.

Part 1 - define product


product [ ]        =  1
product [ x : xs ] =  x * ( product xs )

Now I originally had product [ ] = 0, but realised quickly as I was doing my working out that anything multiplied by zero equals zero, and therefore the identity of multiplication is 1. Therefore the product of the empty list must be 1.

Part 2 - proof that product [ 2, 3, 4 ] = 24.


    product [ 2, 3, 4 ]
 =     { applying product }
    2 * ( product [ 3, 4 ] )
 =     { applying product }
    2 * ( 3 * ( product [ 4 ] ) )
 =     { applying product }
    2 * ( 3 * ( 4 * ( product [ ] ) ) )
 =     { applying product }
    2 * ( 3 * ( 4 * 1 ) )
 =     { applying * }
    24

Marked as Correct, but with the note that the identity of multiplication is 1.

Exercise 4 - How should the definition of the function qsort be changed so that it produces a reverse sorted version of the list? 

I got this correct from a logic point of view, but I think my lecturer (if I had one) might have mentioned that did a bit more than was asked, I defined a new function rqsort (for reverse qsort) rather than just showing the change I would have made to qsort. Not incorrect but not exactly what was asked.


rqsort [ ]        =  [ ]
rqsort [ x : xs } =  rqsort larger ++ [ x ] ++ rqsort smaller
                     where
                        smaller = [ a | a <- xs, a <= x ]
                        larger  = [ b | b <- xs, b > x ]

Correct

Exercise 5 - What would be the effect of replacing <=  by < in the definition of qsort?

The effect of replacing <= by < in the definition of qsort would be that any elements from the input list would only appear a single time in the resultant list. This is because we are removing the the logic that allows elements of equal value to the first element to be added to the resultant list.

Marked as Correct.

So that's five out of five for my first lesson, but with a few notes to take on board for next week. 

Tags: ,

Haskell