|
Volume 1 Issue 2 - Autumn 2001 |
![]()
| Clem Rutter continues to argue his case for Logo to be considered as a teaching language by demonstrating how it can be used to write more complex and relevant applications. |
Introduction
The computer programs we get our pupils to produce are so terribly academic or totally artificial. It wasn’t too long ago that programming was fun and produced results that we wanted - programs that we actually used. Now, the fun bit of computing, the creative bit, is using applications. Who can compete with Excel, Access or Photoshop? Well Logo can. Logo can be fun if you take it seriously, and Logo can produce code that you will want to use.
| I have a digital camera and just using it myself I have produced 2,540 jpegs in one year and I would dearly like to view them, or at least do something with them. No commercial program allows me to catalogue or archive them in a way that I can use, and indeed I keep on changing my mind as to the format I require. This is further hampered by the variety of resolutions of the monitors I use. So flash of genius - why not write a Logo program that generates raw HTML which sources all the selected pictures onto a web page? | ![]() |
The Problem Defined
An early design decision has to be taken on whether you want to have user interaction or just a simple command line. I have gone for the simple command line, because I want to keep it simple, and as I have total control of my source code, I can hack that if needs be.
Writing files
Like Pascal, LOGO has one text output stream. Normally this
goes to the Text Screen in the Commander, but this can be simply changed by
opening a new stream, and setting it for write.
openwrite "album.htm
setwrite "album.htm
This can be and must be reversed to revert to the screen.
closeall setwrite []
Lists
LOGO has a series of procedures and primitives to process
lists, stacks and queues, and indeed when you type the files command, LOGO
returns a LIST of files found in the current working directory in a real list
of words.
pr files
To create an empty list
make "piclist []
To add an item to the end of a list (last).
make "piclist lput ? :piclist]
To get the first item from the list
make “newword first :piclist
To get a numbered item from a list, in this case the fourth.
make “newword item 4 :piclist
To get every item from the list we tend to use a template control structure called foreach. This does what it says and extracts in turn every item from the list and applies to it the commands in the following template. ? represents the word currently in focus and is called a slot filler. # represents ?’s numerical position in the list.
So to get every item from the list generated by files, check if it has the
correct file type and then write it to a new list.
foreach files [if ftype ? [.jpg .gif .bmp ] [make "piclist lput ? :piclist] ]
ftype is a procedure I have written.
Words and error checking.
Everything that you can do to a list in LOGO can also be done to a word, so all the above procedures can be used to manipulate the letters in a word. In addition we have a predicate to test if a substring exists in a word and we can use this in our file selection procedures and our error trapping.
substringp
HTML
As part of our KS3 work we show our students that all HTML files must have the same structure.
<HTML>
<HEAD>
<TITLE>Tigger’s Home Page</TITLE>
</HEAD>
<BODY>
Tigger lives Here.
</BODY>
</HTML>
It is easy to get LOGO to write this code to the output stream usually the screen.
pr [<HTML>]
pr [<HEAD>]
pr [<TITLE>Tigger’s Home Page</TITLE>]
pr [</HEAD>]
pr [<BODY>]
pr [Tigger live Here.]
pr [</BODY>]
pr [</HTML>]
The HTML code needed to include an image has to be:
<img src=”tigger.jpg” alt=”tigger” > tigger.jpg
In the Logo version we wish to use a variable to store the image and the name of the image. It would have to be:
pr [<img src="] pr :image pr [" alt="] pr :image pr ["><br>]
pr :image pr [<p>]
In this application the variables are stored in a list :piclist
and again we use the simple ‘template’ control structure foreach to access
them. More details on foreach are stored in the Logo help file- type help “foreach.
foreach :piclist [pr [<img src="] pr ? pr [" alt="] pr ? pr ["><br>] pr ? pr [<p>] ]
We control the height of the image by using the height= attribute. By defining this we can also control the width of the image and thus the number of images displayed on each line.
foreach :piclist [pr [<img src="] pr ? pr [" alt="] pr ? pr [" height="800"><br>] pr ? pr [<p>] ]
As I wish to have more than one format generated I have merely used a flight of if statements to select the final code. Being fair, I don’t claim that Logo always produces the most readable code. Here if takes 2 arguments, a condition and an action. equalp is the condition, it takes two arguments :depth and 1 and returns “true or “false. If it returns “true then the action in the brackets (the list of commands) is run.
if equalp :depth 1 [foreach :piclist [pr [<img src="] pr ? pr [" alt="] pr ? pr [" height="800"><br>] pr ? pr [<p>] ] ]
| As you will see here we have a working utility written in Logo that is entirely devoid of ‘Turtle Graphics”. But what have I written? - Is it a Dedicated HTML Generator, or is it A Graphics Browser? – someone please tell me. It is definitely a serious piece of LOGO and a heavily used utility- and yes it is fun. | ![]() |
Targets for the Future
Perfective Maintenance
Corrective Maintenance
The next stage is to correct the code so that the file names print out correctly in the 4, 6 and 8 options, you will need to examine the # option in foreach, and the item procedure to get a numbered item from a list.
Adaptive Maintenance
The next stage is to modify the code so I can pull out of each directory all the files starting Aug10_ which is the way that Fuji Names its files, or ac*.gif to isolate the graphics I have pulled down from the Free Graphics Store Website.
Attach the code to a Windows GUI.
Use the <table></table> facility to ensure web pages were exactly A4 to facilitate printing.
A simple HTML generator, with site management facilities?
Clem Rutter is an elected member of Medway Council and teaches IT at Swan Valley near Dartford, Kent. (2001-2002)
to aaa
; This program generates HTML code It reads all the graphic file
; names fron a given directory then writes these names into a file
; ALBUM.HTM in the correct code so that it can be displayed on
; screen or on paper. The exact format is decided by the format of
; the photos, and the guide number 1 2 4 ; 6 or 8to use type:
; main 2 "c:\\home\\jfk
; Note the use of \\ instead of \ when refering to directories.
; Note the use of the quote " infront of the directory name.
; Note ". uses the current directory
end
to albumpics :n :d
main :n :d
end
to ftypep :candidate :testlist
foreach :testlist [ if substringp ? :candidate [ op "TRUE ] ]
op "FALSE
end
to main :depth :basedirectory
if emptyp :depth [make "depth 2]
ifelse ftypep :depth [1 2 4 6 8] [] [make "depth 2 ]
if emptyp :basedirectory [make "basedirectory ".]
if emptyp files [stop]
;============================================================
chdir :basedirectory ;copy the selected files to :pictlist
make "piclist []
foreach files [if ftypep ? [.jpg .gif .bmp ] [make "piclist lput ? :piclist] ]
pr :piclist pr files
;============================================================
;set the writer to the file album.htm
openwrite "album.htm
setwrite "album.htm
;============================================================
;now do the code
pr [<html>] pr [<head><title>]
pr [ALBUMPICS - Images in folder]pr :basedirectory
pr [</title></head><center>]
pr [<body>]
if equalp :depth 1 [foreach :piclist [pr [<img src="] pr ? pr [" alt="] pr ? pr [" height="800"><br>] pr ? pr [<p>] ] ]
if equalp :depth 2 [foreach :piclist [pr [<img src="] pr ? pr [" alt="] pr ? pr [" height="400"><br>] pr ? pr [<p>] ] ]
if equalp :depth 4 [foreach :piclist [pr [<img src="] pr ? pr [" alt="] pr ? pr [" height="400">] ] pr [<br>] pr :piclist ]
if equalp :depth 6 [foreach :piclist [pr [<img src="] pr ? pr [" alt="] pr ? pr [" height="266">] ] pr [<br>] pr :piclist ]
if equalp :depth 8 [foreach :piclist [pr [<img src="] pr ? pr [" alt="] pr ? pr [" height="200">] ] pr [<br>] pr :piclist ]
pr [<hr>This file generated by the program<br><font size="4">ALBUMPICS</font><br>]
pr [<br>- Freeware- Written by C.Rutter - in MSWLOGO -<br>]
pr [</body>]pr [</html>]
;================================================================
closeall setwrite []
pr [Done..Thank you.]
end

|
Volume 1 Issue 2 - Autumn 2001 |