Myopic View of scheme
Friday, January 27, 2006
Wednesday, January 18, 2006
Lists and Maps
Lists and Maps
The simplest way to define a list is
If we want a function that works on each member of this list, then we will have to make use of the map inbuilt function.
Here is a function that adds 1 to each member of a list and then returns that list
We can play around a bit more with map
Let us define a function that adds a fixed number to a-list
We can refine this further to take any list and add a function that does a minus as well
Then we can call the functions as
We can even make list-plus-n and list-minus-n to be wrappers for a more generic function
The simplest way to define a list is
(define a-list '(1 2 3 4 5 6 7 8))
If we want a function that works on each member of this list, then we will have to make use of the map inbuilt function.
Here is a function that adds 1 to each member of a list and then returns that list
(define (add-one n)
(+ n 1))
(map add-one a-list)
We can play around a bit more with map
Let us define a function that adds a fixed number to a-list
(define (list-plus n)
(map (lambda (x)
(+ x n))
a-list))
We can refine this further to take any list and add a function that does a minus as well
(define (list-plus-n n some-list)
(map (lambda (x)
(+ x n))
some-list))
(define (list-minus-n n some-list)
(map (lambda (x)
(- x n))
some-list))
Then we can call the functions as
(list-plus-n 10 a-list)
(list-minus-n 10 a-list)
We can even make list-plus-n and list-minus-n to be wrappers for a more generic function
(define (list-plus-n n some-list)
(list-map + n some-list))
(define (list-minus-n n some-list)
(list-map - n some-list))
(define (list-map operation n some-list)
(map (lambda (x)
(operation x n))
some-list))
Saturday, January 07, 2006
Celsius to Fahrenheit in Scheme
Converting from C to F is trival - just multiple the temperature in C with 9/5 and then add 32.
So, a scheme version of this would look something like this
However, this gives results out as fractions. So, (celsius-fahrenheit 32) gives the result as
Therefore, the correct implementation would be
This gives the result as
Ideally, this function would be using recursion instead a loop to go through all the
functions.
However, the display bit can also be moved out to a separate function
Then, our function will look like this
But the biggest change for a C program is the fact that functions are first class objects. What this means, is that we can use functions in interesting ways. We can define separate functions for celsius to fahrenheit conversion and vice versa, and pass them around. That means, we have a generic function that prints a table.
If you are interested in learning more about scheme then schemers.org is a great starting point. I also have a page maintaining some of my posts on scheme.
So, a scheme version of this would look something like this
(define (celsius-fahrentheit c)
(+ (* c (/ 9 5)) 32))
However, this gives results out as fractions. So, (celsius-fahrenheit 32) gives the result as
448/5
Therefore, the correct implementation would be
(define (celsius-fahrentheit c)
(+ (* c (/ 9.0 5.0)) 32.0))
This gives the result as
89.6.We can also define a new function that prints a table of celsius and fahrenheit values.
Ideally, this function would be using recursion instead a loop to go through all the
functions.
(define (celsius-fahrenheit-table min-value max-value)
(let ((c min-value))
(if (<= c max-value) (begin (display c) (display "<->")
(display (celsius-fahrenheit c))
(newline))
(celsius-fahrenheit-table (+ min-value 1) max-value))
0)))->
However, the display bit can also be moved out to a separate function
(define (display-items x y)
(display y)
(display "<->")
(display (x y))
(newline))->
Then, our function will look like this
(define (celsius-fahrenheit-table min-value max-value)
(let ((c min-value))
(if (<= c max-value)
(begin
(display-items celsius-fahrenheit c)
(celsius-fahrenheit-table (+ min-value 1) max-value)) 0)))
But the biggest change for a C program is the fact that functions are first class objects. What this means, is that we can use functions in interesting ways. We can define separate functions for celsius to fahrenheit conversion and vice versa, and pass them around. That means, we have a generic function that prints a table.
(define (tabulate convertor min-value max-value)
(define (display-items x y)
(display y) (display "<->")
(display (x y))
(newline))
(let ((x min-value))
(if (<= x max-value)
(begin
(display-items convertor x)
(tabulate convertor (+ x 1) max-value))
0)))
(define (celsius-fahrenheit-table min-value max-value)
(tabulate celsius-fahrenheit min-value max-value))
(define (fahrenheit-celsius-table min-value max-value)
(tabulate fahrenheit-celsius min-value max-value))->
If you are interested in learning more about scheme then schemers.org is a great starting point. I also have a page maintaining some of my posts on scheme.
Sunday, January 01, 2006
Hello World
The first thing to do is to install a scheme implementation and run a "hello world" program. So, I downloaded MIT/GNU Scheme and installed it on my Laptop. MIT/GNU Scheme can be downloaded from here.
I ran the following code
Both versions print hello world, and in fact the first version is a recursive loop that continuously prints hello world. More importantly, both versions did not give the above error.
I ran the following code
(display "Hello World")This ran fine, but gave an error
;Unspecified return valueThis required some additional searching on the net. A quick search on google gave me two different links. The first is from a website that has "Hello World" Programs in a zillion languages and the other is from GNU on how people from different age groups and jobs write a "Hello World" Program.
Both versions print hello world, and in fact the first version is a recursive loop that continuously prints hello world. More importantly, both versions did not give the above error.
Why should I learn Scheme?
As a software engineer, I'm not unique for not having never learnt Scheme or Common Lisp. I had my Professional Education in Electronics and Communication Engineering, where the focus had been on learning stuff far removed from my day to day work - which is Software Engineering.
As a result, not knowing some of the basics of computer science has left me with a poorer understanding of software. To make up for this gap, one of my new year's resolution is to learn Scheme. Learning Scheme by itself will not make up for my lack of education, but will help in understanding some of the fundamentals that I sorely lack.
This blog is about Scheme and related topics. I'm going to look at Scheme through a Newbie's eyes. In my day to day job, I work exclusively on Embedded Software, which requires good understanding of Operating Systems, C, Hardware/Software Interfaces, Device Drivers. At this point in time, I don't see any connection between Scheme and my day to day work, but I hope to learn it nevertheless. Its been almost 8 years since I first learnt C, and over 10 years since I learnt Fortran. But I learnt my first language, Basic almost 14 years ago. Its about time I learnt Scheme.
As a result, not knowing some of the basics of computer science has left me with a poorer understanding of software. To make up for this gap, one of my new year's resolution is to learn Scheme. Learning Scheme by itself will not make up for my lack of education, but will help in understanding some of the fundamentals that I sorely lack.
This blog is about Scheme and related topics. I'm going to look at Scheme through a Newbie's eyes. In my day to day job, I work exclusively on Embedded Software, which requires good understanding of Operating Systems, C, Hardware/Software Interfaces, Device Drivers. At this point in time, I don't see any connection between Scheme and my day to day work, but I hope to learn it nevertheless. Its been almost 8 years since I first learnt C, and over 10 years since I learnt Fortran. But I learnt my first language, Basic almost 14 years ago. Its about time I learnt Scheme.
Subscribe to:
Posts (Atom)