Saturday, December 22, 2012

The Last of the Big 3 Higher-Order Functions - Folding

In functional programming, the big 3 higher-order functions are: mapping, filtering, and folding.  This blog post is on the last, folding.

Folding is typically used to apply a function to a list of data obtaining a single result.  Consider the summing up of a list of numbers, the addition routine could be applied using a fold against a list yielding a single number as the result.




Given the image above, we have a list of numbers (9, 4, 2, 4) and a magenta function "Adder" which takes the list of numbers and applies the adder function against them giving us the single output value of 19.

The way in which folding takes place can be thought of as the following.

  1. Adder 9 4 = 13
  2. Adder 13 2 = 15
  3. Adder 15 4 = 19
Or if you dislike that manner of notation.
  1. 9 + 4 = 13
  2. 13 + 2 = 15
  3. 15 + 4 = 19

Typically in functional programming languages there are many different ways to fold a function against a list.  In the F# examples which will follow, we'll use fold and reduce, the only difference between the two (at least the only one I know of) is that fold requires an initial value for the function to be applied to, while reduce uses the head of the list as the initial value.

Note that we are using fsi.exe running on Mono.

Given a list of number (x), sum up the values (using 0 as the initial value)


> let x = [4398; 233; 1; 34; 42; 8392; -8];;

val x : int list = [4398; 233; 1; 34; 42; 8392; -8]

> x |> List.fold (+) 0
- ;;
val it : int = 13092


Using reduce gives the same value (and does not require an initial value)


> let x = [4398; 233; 1; 34; 42; 8392; -8];;

val x : int list = [4398; 233; 1; 34; 42; 8392; -8]

> x |> List.fold (+) 0
- ;;
val it : int = 13092
> x |> List.reduce (+)
- ;;
val it : int = 13092


Similarly, we can use folding to find the max and min values (same x as before).


> let x = [4398; 233; 1; 34; 42; 8392; -8];;

val x : int list = [4398; 233; 1; 34; 42; 8392; -8]

> x |> List.reduce (max)
- ;;
val it : int = 8392
> x |> List.reduce (min)
- ;;
val it : int = -8


As you have witness from the code above, folding can be used to apply a function which takes two arguments against a list of values producing one resulting value.  Folding is a very useful tool in the functional programming tool belt and is one of the big 3 higher-order functions.


Sunday, December 16, 2012

Evens with a Filter

One of the most common "tools" in the functional programming tool belt is the filter.  A filter is typically a function which applied to remove elements a list.



Given the image above, the light green filter called Evens which filters out the member of the list which are not even, in this case out of the list [3; 4; 5; 6] only the members [4; 6] survive the filter.

Note in a language like F# which is immutable the list the filter returns is a new list.

If we want to filter out all of the odd numbers from 1 to 100 we could do the following in F# (using fsi.exe running against Mono).


> let x = [ 1 .. 100 ];;

val x : int list =
  [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21;
   22; 23; 24; 25; 26; 27; 28; 29; 30; 31; 32; 33; 34; 35; 36; 37; 38; 39; 40;
   41; 42; 43; 44; 45; 46; 47; 48; 49; 50; 51; 52; 53; 54; 55; 56; 57; 58; 59;
   60; 61; 62; 63; 64; 65; 66; 67; 68; 69; 70; 71; 72; 73; 74; 75; 76; 77; 78;
   79; 80; 81; 82; 83; 84; 85; 86; 87; 88; 89; 90; 91; 92; 93; 94; 95; 96; 97;
   98; 99; 100]

> let evens x =
-   match x with
-   | x when x % 2 = 0 -> true
-   | _ -> false;;

val evens : int -> bool

> x |> List.filter evens
- ;;
val it : int list =
  [2; 4; 6; 8; 10; 12; 14; 16; 18; 20; 22; 24; 26; 28; 30; 32; 34; 36; 38; 40;
   42; 44; 46; 48; 50; 52; 54; 56; 58; 60; 62; 64; 66; 68; 70; 72; 74; 76; 78;
   80; 82; 84; 86; 88; 90; 92; 94; 96; 98; 100]


In our code above we have a list x which has the values 1 through 100 in it.  We likewise have a function called evens which returns true or false based on if the value passed in is divisible by 2.  We conclude with List.filter utilizing our function evens against every member of list x, if evens returns true that element of x is passed to the output list.  Hence with any function which returns a bool, we can encumber that function to a list using List.filter and filter out any undesirable elements.

Getting Sound to Work Again on Ubuntu 12.04

Well, it happen again--I did what I thought would be some simple updates on my Dell Vostro 1500 running Ubuntu 12.04 and boom--no sound!  I did a little googling and found this very useful write up.

I was unable to get anything to work (but I understood what the issue was by following the steps in the write up), then I come across the section titled: Getting the ALSA drivers from a *fresh* kernel.

I performed the following commands to freshen up my ALSA drivers.


sudo apt-get --purge remove linux-sound-base alsa-base alsa-utils


sudo apt-get install linux-sound-base alsa-base alsa-utils


sudo apt-get install gdm ubuntu-desktop


aplay -l


After running aplay -l and subsequently seeing that my drivers looked secured, I head back to The Casters YouTube video on Terminal and Initial Objects that I want to watch and was happy to listen to audio again!  Thank you LordRaiden!

Sunday, December 9, 2012

FizzBuzz using a mapping

In functional programming one of the most used "tools" is the mapper.  The mapper is typical used to apply a function to elements of a list.



Using the image above we can imagine that we have a mapper which takes elements and changes the color to magenta, thus the blue circle A would be mapped to a magenta circle A, B to B, and C to C.  

Note that only the color has been changed.

If we want to do FizzBuzz in F# using a mapper we could do the following (using fsi.exe running against Mono).

> let x = [ 1 .. 31 ];;

val x : int list =
  [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21;
   22; 23; 24; 25; 26; 27; 28; 29; 30; 31]

> let fizzBuzzer x = 
-  match x with 
-  | x when x % 15 = 0 -> "FizzBuzz"
-  | x when x % 3 = 0 -> "Fizz"
-  | x when x % 5 = 0 -> "Buzz"
-  | x -> x.ToString()
- ;;

val fizzBuzzer : int -> string

> x |> List.map fizzBuzzer;;
val it : string list =
  ["1"; "2"; "Fizz"; "4"; "Buzz"; "Fizz"; "7"; "8"; "Fizz"; "Buzz"; "11";
   "Fizz"; "13"; "14"; "FizzBuzz"; "16"; "17"; "Fizz"; "19"; "Buzz"; "Fizz";
   "22"; "23"; "Fizz"; "Buzz"; "26"; "Fizz"; "28"; "29"; "FizzBuzz"; "31"]

In the code above, our list of numbers is x which holds the numbers 1 through 31.  We also have a function called fizzBuzzer which takes an int and outputs a string based on the typical FizzBuzz rules.  Last we use List.map to do a mapping of fizzBuzzer against x giving us a list of strings with the FizzBuzz rules applied.

Note, we could have done this very easily in one line of F#.

Thursday, December 2, 2010

How I Fixed Ubuntu Boot Issue with Error Message, "Starting up... Undefined video mode 31f"

I ran some updates on Ubuntu 10.10 and found that when I booted up I got the message, "Starting up... Undefined video mode 31f".  (Sorry I am not sure which update caused this.)

I hit space and allowed it to continue.  I then watch Bleach 296, did a few other things and called it a day.  The next day when I booted up, same issue (I was kind of hoping it was a non-issue issue, O-well).

I googled the error message and found this post, which made perfect sense to me (I also found a lot of other posts that made no sense and gave no real fixes).  Following what was in post in the form, I did the following:

apt-get install hwinfo
sudo hwinfo --framebuffer

Giving me:

02: None 00.0: 11001 VESA Framebuffer                        
  [Created at bios.464]
  Unique ID: rdCR.zrHRJLheAS6
  Hardware Class: framebuffer
  Model: "NVIDIA G84 Board - p410h1b "
  Vendor: "NVIDIA Corporation"
  Device: "G84 Board - p410h1b "
  SubVendor: "NVIDIA"
  SubDevice:
  Revision: "Chip Rev"
  Memory Size: 14 MB
  Memory Range: 0xfb000000-0xfbdfffff (rw)
  Mode 0x0300: 640x400 (+640), 8 bits
  Mode 0x0301: 640x480 (+640), 8 bits
  Mode 0x0303: 800x600 (+800), 8 bits
  Mode 0x0305: 1024x768 (+1024), 8 bits
  Mode 0x0307: 1280x1024 (+1280), 8 bits
  Mode 0x030e: 320x200 (+640), 16 bits
  Mode 0x030f: 320x200 (+1280), 24 bits
  Mode 0x0311: 640x480 (+1280), 16 bits
  Mode 0x0312: 640x480 (+2560), 24 bits
  Mode 0x0314: 800x600 (+1600), 16 bits
  Mode 0x0315: 800x600 (+3200), 24 bits
  Mode 0x0317: 1024x768 (+2048), 16 bits
  Mode 0x0318: 1024x768 (+4096), 24 bits
  Mode 0x031a: 1280x1024 (+2560), 16 bits
  Mode 0x031b: 1280x1024 (+5120), 24 bits
  Mode 0x0330: 320x200 (+320), 8 bits
  Mode 0x0331: 320x400 (+320), 8 bits
  Mode 0x0332: 320x400 (+640), 16 bits
  Mode 0x0333: 320x400 (+1280), 24 bits
  Mode 0x0334: 320x240 (+320), 8 bits
  Mode 0x0335: 320x240 (+640), 16 bits
  Mode 0x0336: 320x240 (+1280), 24 bits
  Mode 0x033d: 640x400 (+1280), 16 bits
  Mode 0x033e: 640x400 (+2560), 24 bits
  Mode 0x0345: 1600x1200 (+1600), 8 bits
  Mode 0x0346: 1600x1200 (+3200), 16 bits
  Mode 0x0347: 1400x1050 (+1400), 8 bits
  Mode 0x0348: 1400x1050 (+2800), 16 bits
  Mode 0x0349: 1400x1050 (+5600), 24 bits
  Mode 0x034a: 1600x1200 (+6400), 24 bits
  Mode 0x0352: 2048x1536 (+8192), 24 bits
  Mode 0x0360: 1280x800 (+1280), 8 bits
  Mode 0x0361: 1280x800 (+5120), 24 bits
  Mode 0x0362: 768x480 (+768), 8 bits
  Mode 0x0364: 1440x900 (+1440), 8 bits
  Mode 0x0365: 1440x900 (+5760), 24 bits
  Mode 0x0368: 1680x1050 (+1680), 8 bits
  Mode 0x0369: 1680x1050 (+6720), 24 bits
  Mode 0x037c: 1920x1200 (+1920), 8 bits
  Mode 0x037d: 1920x1200 (+7680), 24 bits
  Config Status: cfg=new, avail=yes, need=no, active=unknown

I figured I'd use 0x0369

I then edited grub using:

sudo gedit /boot/grub/menu.lst

I look for the "vga=" and change it to the mode value I wanted to use (0x0369)

## ## End Default Options ##


title Ubuntu 10.10, kernel 2.6.35-23-generic-pae
root (hd0,6)
kernel /boot/vmlinuz-2.6.35-23-generic-pae root=UUID=3608162f-dcd7-4b92-b1d9-c2162c720162 ro quiet vga=0x0369 
initrd /boot/initrd.img-2.6.35-23-generic-pae
quiet


Restarted and now no more error message!