First Steps with Free Pascal – The currency converter

Introduction:
I was lately blessed with an array of coursework assignments for a module called “Paradigms of Programming”. The language I picked to get into imperative programming was Pascal and I had a few fun weekend with it programming a currency converter.
I’m hoping others might find the resulting code helpful or interesting :)
Preparations:

 

The Code – Stage 1: Source Currency
Step 1: Constants and variables
We need to initialising a few constants for the currency symbols
const
euro = ‘€’;
pound = ‘£’;
dollar = ‘$’;

And now we initialise some variables to hold temporary results of each step
var
step_one : integer;
step_one_cur : string;
step_two : real;
step_three : integer;
step_three_cur : string;

Step 2: Let’s get the user to enter the source currency
writeln (‘Welcome to the exciting world of Pascal currency conversion!’);
writeln (‘This program will convert between the following currencies for you:’);
writeln (euro, ‘,’, pound, ‘,’, dollar);
writeln (‘——————————————————-’);
writeln (‘Step 1 – Please pick the currency of which you have notes/coins from the following list by entering 1,2 or 3 and hitting enter:’);
writeln (’1: ‘, euro);
writeln (’2: ‘, pound);
writeln (’3: ‘, dollar);
readln (step_one);

Step 3: To reassure our user we tell him what we think he just entered for step one but end the program if the user picks anything apart from 1-3
case step_one of
1:
begin
step_one_cur := euro;
writeln (‘You have selected ‘, step_one_cur, ‘ as your source currency.’)
end;
2:
begin
step_one_cur := pound;
writeln (‘You have selected ‘, step_one_cur, ‘ as your source currency.’)
end;
3:
begin
step_one_cur := dollar;
writeln (‘You have selected ‘, step_one_cur, ‘ as your source currency.’)
end;
otherwise writeln (‘Only options 1-3 are available for Step 1, please try again.’);
exit;
end;

 

The Code – Stage 2: Source amount
Step 1: Let’s get the user to enter the source amount but end the program if the user enters a negative amount
writeln (‘——————————————————-’);
writeln (‘Step 2 – Please enter the amount of ‘, step_one_cur, ‘s you have and press enter:’);
readln (step_two);
{end program if the user enters a negative source amount}
if step_two < 0 then
begin
writeln (‘You have entered a negative amount of ‘, step_two:3:2, step_one_cur, ‘, please try again’);
exit;
end;

Step 2: To reassure our user we tell him what we think he just entered for step two
writeln (‘You have entered: ‘, step_two:3:2, step_one_cur);

 

The Code – Stage 3: Target currency

Step 1: Let’s get the user to enter the target currency but end the program if the user enters the same currency we have from Stage 1 as source currency or picks outside the 1-3 range
writeln (‘——————————————————-’);
writeln (‘Step 3 – Please pick the currency you want your ‘, step_two:3:2, step_one_cur, ‘ converted to by entering 1,2 or 3 and hitting enter:’);
writeln (’1: ‘, euro);
writeln (’2: ‘, pound);
writeln (’3: ‘, dollar);
readln (step_three);

if step_one = step_three then
begin
writeln (‘You have picked the same currency for step 1 and 3, please try again’);
halt;
end;

Step 2: To reassure our user we tell him what we think he just entered for step three}
case step_three of
1:
begin
step_three_cur := euro;
writeln (‘You have selected ‘, step_three_cur, ‘ as your target currency.’)
end;
2:
begin
step_three_cur := pound;
writeln (‘You have selected ‘, step_three_cur, ‘ as your target currency.’)
end;
3:
begin
step_three_cur := dollar;
writeln (‘You have selected ‘, step_three_cur, ‘ as your target currency.’)
end;
otherwise writeln (‘Only options 1-3 are available for Step 1, please try again.’);
exit;
end;

 

The Code – Stage 4: Let’s do some converting and feed source amount, source currency and target currency from the previous steps into the step four procedure
writeln (‘——————————————————-’);
Step 1: To reassure our user we tell him what we think he entered for step 1-3
writeln (‘Step 4 – If everything went well you want ‘, step_two:3:2, step_one_cur, ‘ converted to ‘, step_three_cur);

Step 2: Feed step_four procedure
step_four (step_one_cur, step_two, step_three_cur)
end.

 

The Code – Stage 5: Calculate the result

Step 1: Definition, constants and variables
First we define what our step_four procedure get’s fed with
procedure step_four (source_cur : string; source_amount : real; target_cur : string);
Then we initialise the variables for the currency symbols
var euro : string;
var pound : string;
var dollar : string;
Initialise variables for the conversion rates
var euro2pound : real;
var euro2dollar : real;
var pound2euro : real;
var pound2dollar : real;
var dollar2euro : real;
var dollar2pound : real;
Initialise the temp variable which will hold the final result for step five
var step5 : real;

Step 2: Fill variables
We fill the variables we initialised above with currency symbols, conversion rates and set step five temp variable to zero
euro := ‘€’;
pound := ‘£’;
dollar := ‘$’;
euro2pound := 0.8;
euro2dollar := 1.3;
pound2euro := 1.2;
pound2dollar := 1.6;
dollar2euro := 0.8;
dollar2pound := 0.6;
step5 := 0;

Step 3: Picking the right conversion rate
Depending on source and target currency we tell the user the conversion rate and assign the result to our step five variable
if (source_cur = euro) and (target_cur = pound) then
begin
writeln (‘The exchange rate for euro to pound is: ‘, euro2pound:3:2);
step5 := source_amount*euro2pound
end;

if (source_cur = euro) and (target_cur = dollar) then
begin
writeln (‘The exchange rate for euro to dollar is: ‘, euro2dollar:3:2);
step5 := source_amount*euro2dollar
end;

if (source_cur = pound) and (target_cur = euro) then
begin
writeln (‘The exchange rate for pound to euro is: ‘, pound2euro:3:2);
step5 := source_amount*pound2euro
end;

if (source_cur = pound) and (target_cur = dollar) then
begin
writeln (‘The exchange rate for pound to dollar is: ‘, pound2dollar:3:2);
step5 := source_amount*pound2dollar
end;

if (source_cur = dollar) and (target_cur = euro) then
begin
writeln (‘The exchange rate for dollar to euro is: ‘, dollar2euro:3:2);
step5 := source_amount*dollar2euro
end;

if (source_cur = dollar) and (target_cur = pound) then
begin
writeln (‘The exchange rate for dollar to pound is: ‘, dollar2pound:3:2);
step5 := source_amount*dollar2pound
end;

 

The Code – Stage 6: Spit out the whole lot

Let’s tell our user how much X of Y he would get for his A of B at the current conversion rate of C}
writeln (‘——————————————————-’);
writeln (‘Step 5 – Results:’);
writeln (‘At the current exchange rate you would get ‘, step5:3:2, target_cur, ‘ for your ‘, source_amount:3:2, source_cur);

Pascal Currency Converter

 

 

Conclusion:
Yes, I admit growing up with Pascal and C so this brings back fond memories of sitting in a dark cellar computer lab in a school in Munich :) I can recommend Pascal to anyone who wants to get into imperative programming, it’s easy enough to pick up and once one hit’s it’s limitations it’s easy to move on to mighty old C.

 

 

 

 

 

FacebookGoogle GmailEvernoteDeliciousShare

First steps with Haskell – The word counter

Introduction:
I was lately blessed with an array of coursework assignments for a module called “Paradigms of Programming”. The language I picked to get into functional programming was Haskell and I had a few fun afternoons with it programming a word counter which counts both the total number of words as well as the frequency of words in a given text file.
I’m hoping others might find the resulting code helpful or interesting :)

Preparations:

  • Get Haskell
  • Get MissingH from here
  • For an overview of Haskell have a look at Wikipedia
  • If you are trying Haskell for the first time you might want to spend a few minutes with this interactive tutorial
  • This is my source code and count.txt file for the example below: Haskell word counter – source/count.txt
  • If you want to know more about the commands I used click on the commands in the code below and they’ll send you to the Haskell code reference for the command

 

The Code – Stage 1: Word count
Step 1: Let’s be nice and talk to the human:
putStrLn “Welcome to the exciting world of Haskell word counting!”
putStrLn “Please enter the full name of the file which contains the words you want counted:”

Step 2: Then we get the file name from our user and store it in “name”
name <- getLine
putStrLn $ “The file name you have entered is: ” ++ name

Step 3: let’s read the file specified in “name” into “contents”
contents <- readFile name

Step 4: let’s get rid of punctuation (e.g. . or !) which are currently attached to the end of words (thishadafullstop. -> thishadafullstop)
let contents2 = replace “.” “” contents
let contents3 = replace “!” “” contents2

Step 5: let’s make the words all lower case to ensure .Later = later
let lower = map toLower contents3

Step 6: let’s feed the files content to “words” which will chop it down and return us a nice clean array of the contained words
let chop = words (lower)

Step 7: let’s take the result of step 4 and feed it to “length” which will return the number of words in the array and add a “/n” at the end to make it pretty…
let count = show (length chop) ++ “\n”

Step 8: let’s throw a pretty result at the human crouched in front of the screen
putStrLn $ “This wonderful Haskell program has found ” ++ count ++ “words in count.txt”

 

The Code – Stage 2: Word frequency

Step 1: let’s “sort” that lot and get all the identical words together (Example: ["a","a","a","a","a","about","also","an","and","and","and","and","and","api"])
let chop2 = sort chop

Step 2: now we “group” all instances of each word into a separate list (Example: [["a","a","a","a","a"],["about"],["also"],["an"],["and","and","and","and","and"],["api"]])
let chop3 = group chop2

Step 3: for each list x we use “length” to see how many instances we’ve got in the list and “head” to get one example of content (Example: ["a","a","a","a","a"],["about?"] -> [5,"a"], [1,"about"])
let chop4 = map (\x -> (length x, head x)) chop3

Step 4: “reverse $ sort” will sort our list with the most frequent words at the beginning and descending order
let chop5 = reverse (sort chop4)

 

Stage 3: Let’s filter out the Top20 words in our document

Step 1: Exclusions, not much point in building a top20 list without this step unless we want to prove that this will be very similar across most longer texts… (http://en.wikipedia.org/wiki/Most_common_words_in_English)
let exclusion_1 = “the”
let exclusion_2 = “be”
let exclusion_3 = “to”
let exclusion_4 = “of”
let exclusion_5 = “and”
let exclusion_6 = “a”
let exclusion_7 = “in”
let exclusion_8 = “that”
let exclusion_9 = “have”
let exclusion_10 = “I”
let exclusion_11 = “it”
let exclusion_12 = “for”
let exclusion_13 = “not”
let exclusion_14 = “on”
let exclusion_15 = “with”
let exclusion_16 = “he”
let exclusion_17 = “as”
let exclusion_18 = “you”
let exclusion_19 = “do”
let exclusion_20 = “at”

Step 2: with “filter” we can take out all the lists where the second tuple matches one of our exclusions
let chop6 = filter (\(x,y)-> y /= exclusion_1 && y /= exclusion_2 && y /= exclusion_3 && y /= exclusion_4 && y /= exclusion_5 && y /= exclusion_6 && y /= exclusion_7 && y /= exclusion_8 && y /= exclusion_9 && y /= exclusion_10 && y /= exclusion_11 && y /= exclusion_12 && y /= exclusion_13 && y /= exclusion_14 && y /= exclusion_15 && y /= exclusion_16 && y /= exclusion_17 && y /= exclusion_18 && y /= exclusion_19 && y /= exclusion_20) chop5

Step 3: let’s take the top20 words so we don’t throw an endless amount of stuff at our user
let chop7 = take 20 chop6

Step 4: let’s throw another pretty result at the human crouched in front of the screen
putStrLn $ “This wonderful Haskell program has also had a look at the ” ++ count ++ “words in ” ++ name ++ ” and after excluding the 20 most frequently used words as defined by wikipedia these are the most frequently used words in ” ++ name ++ “:”
let top20 = show chop7
putStrLn top20

Haskell word counter v2.1

Haskell word counter v2.1

 

Stage 4 will usually be commented out, I’ve merely fed it in to prove that the endless line generates the same output as the chopped up version above…
Stage 4: and just because we can, now all together in one messy line with tons a ()s and $s:

let alltogether = show $ take 20 $ filter (\(x,y)-> y /= exclusion_1 && y /= exclusion_2 && y /= exclusion_3 && y /= exclusion_4 && y /= exclusion_5 && y /= exclusion_6 && y /= exclusion_7 && y /= exclusion_8 && y /= exclusion_9 && y /= exclusion_10 && y /= exclusion_11 && y /= exclusion_12 && y /= exclusion_13 && y /= exclusion_14 && y /= exclusion_15 && y /= exclusion_16 && y /= exclusion_17 && y /= exclusion_18 && y /= exclusion_19 && y /= exclusion_20) (reverse $ sort $ map (\x -> (length x, head x)) (group $ sort $ words $ map toLower (replace “.” “” (replace “!” “” contents))))
putStrLn alltogether

 

Conclusion:

Yes, I admit growing up with Pascal and C :) Having said that, I’ve spent a lot of time with SQL over the last couple of years and parts of Haskell like “sort” and “group” felt very intuitive. Just like SQL or OCaml it took a little while to get into the flow of how things are done but the amount of testing required is much lower than with any imperative language I know which is a big plus on my lazy programmer list ;)

FacebookGoogle GmailEvernoteDeliciousShare

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress


Hit Counter provided by seo company