The Artima Developer Community
Sponsored Link

Weblogs Forum
The adventures of a Pythonista in Schemeland/4

11 replies on 1 page. Most recent reply: Nov 2, 2008 1:22 AM by Grant Rettke

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 11 replies on 1 page
Michele Simionato

Posts: 222
Nickname: micheles
Registered: Jun, 2008

The adventures of a Pythonista in Schemeland/4 (View in Weblogs)
Posted: Oct 1, 2008 7:47 AM
Reply to this message Reply
Summary
Yet another introductory installment. However, after a brief discussion of the available Scheme bibliografy, I will actually start writing real Scheme code ...
Advertisement

Scheme resources for beginners

The present series has the ambition to be a nearly self-consistent complement to the R6RS document. In theory you should be able to learn Scheme by reading my "Adventures" and the R6RS only. In practice, however, having a look at other sources cannot hurt, so I will discuss here a few tutorials/textbooks you can find on the net and which are useful for Scheme beginners. There are many good texts, but none focused on my target of readers, i.e. experienced programmers coming from the scripting language world. I myself am the kind of persons that prefer learning from tutorials, articles and newsgroups more than from books, therefore I am not an expert on the existing bibiliografy. Here I will cite only a few resources, readers more knowledgeable are invited to post their recommendations as comments.

http://www.phyast.pitt.edu/~micheles/scheme/books.jpg

The main reference I used to learn Scheme when I started is Teach Yourself Scheme in Fixnum Days by Dorai Sitaram, which has many good things going for it. It is a self-consistent tutorial on Scheme which is very well written, especially for the first part. It is very informative, but at the same time concise and readable by hobbyist Scheme programmers like myself, i.e. by people using another language at work and not having too much free time at their disposal (I suppose that description fits the majority of my readers). On the other hand, Teach Yourself Scheme in Fixnum Days is a bit old as a reference, and it completely ignores the modern Scheme macrology, based on pattern matching. It only describes the traditional macrology based on define-macro, which many seasoned Schemers do not love. My aim in this series is to give a description of modern Scheme, updated to the R6RS document; moreover I want to discuss in detail modern macros.

A more modern text (not covering the R6RS specification anyway) is The Scheme Programming Language (Third Edition) by R. Kent Dybvig. This is a very good book on Scheme in general. It is also the best reference I have read on syntax-case macros, but it is book with several hundreds of dense pages, perhaps too much for a hobbyist Schemer.

A very recent book is Programming Languages Application and Interpretation, by Shriram Krishnamurthi, which is also excellent, especially the part about continuations, but also very demanding from the reader, since it is a textbook for university students. Notice that even this book does not cover R6RS (to my knowledge there are no text books covering the R6RS standard, since it is too recent).

There is a habit of denoting Scheme books with their initials, so the two books I have just cited are also known as TSPL3 e PLAI; however, the most famous acronymous is certainly SICP, i.e. Structure and Interpretation of Computer Programs, by Harold Abelson e Gerald Jay Sussman. This book has been used to teach Scheme to generations of students and it is considered a cult, but I personally do not know it, therefore I cannot comment. Another book I have not read but I have heard good things about is How to Design Programs by Fellesein, Findler, Flatt and Krishnamurthi, which is a textbook for first year college students. It is up to you to check it and to see if you like it.

As you see, there are plenty of Scheme books, being Scheme a language with a great academical tradition. The problem is not the lack of books, is the lack of time to read them! This is one of the reasons why my Adventures are appearing as blog posts and not as a book: a short paper of 5-6 pages is much less scary than a big book of 500-600 pages. Morevoer blog posts are allowed to keep a much more informal tone than books, so they are both easier to write and to read.

A simple Scheme program

After so much talk, let me show you (finally!) a small example of Scheme program. There is a long tradition of giving the factional function as an example and I do not see a reason to break the tradition. Here is the Scheme code:

;; fac.scm for Chicken Scheme
(define (fac x)
   (if (= x 0) 1
    (* x
     (fac (- x 1)))))

(define n (string->number (car (reverse (argv)))))
(display (fac n))
http://www.phyast.pitt.edu/~micheles/scheme/exclamation.jpg

The equivalent in Python would be:

import sys

def fac(x):
  if x == 0:
    return 1
  else:
    return x * fac(x-1)

n = int(sys.argv[-1])
print fac(n),

This trivial example already prooves what I have been saying all along:

  1. There are lots of parenthesis: five parens at the end of the factorial and four at the end of the definition of n. A typical program contains 3-4 parens per line. It should be noticed that all those parens are useless. By using the SRFI-49 the code could have been written as

    define fac
       if (= x 0) 1
        * x
         fac (- x 1)
    
    define n
     string->number
      car (reverse argv)
    display (fac n)
    
  2. The script is fully non-portable; to my knowledge it only works in Chicken Scheme. The reason is that the R5RS standard DOES NOT SPECIFY any way to read the command line arguments, hence argv is not standard.

    To a Pythonista such a lack looks absurd, but it is only after thirty years that the Schemers have decided how to manage sys.argv in the R6RS standard, which however is still little diffused and probably will remain a minority standard for years to come.

  3. To get the last element of argv, Python uses the standard syntax argv[-1]; there is no standard function syntax to do it in Scheme, therefore or you use a non-portable function, or you reverse the list and you keep the first element with car (if you want to know the origin of the term you may have a look at this Wikipedia article): this is not really readable, but readability never counted much in the Scheme world. Some Scheme implementations accepts the more readable name first as a synonimous of car, but this is again not standard.

  4. The result of fac depends on the implementation: some implementations support infinite precision numbers (this is required by the R6RS) but some implementations do not. In particular in Chicken one gets

$ rlwrap csi
CHICKEN
Version 2.732 - macosx-unix-gnu-x86     [ manyargs dload ptables applyhook cross ]
(c)2000-2007 Felix L. Winkelmann        compiled 2007-11-01 on michele-mac.local (Darwin)
#;1> (define (fac x)  (if (= x 0) 1 (* x (fac (- x 1)))))
#;2> (fac 10)
3628800
#;3> (fac 100)
9.33262154439441e+157
#;4> (fac 1000)
+inf

In Ikarus (which is R6RS-compliant) one gets instead:

$ rlwrap ikarus
Ikarus Scheme version 0.0.2
Copyright (c) 2006-2007 Abdulaziz Ghuloum
> (define (fac (x) (if (= x 0) 1 (* x (fac (- x 1))))))
> (fac 10)
3628800
> (fac 100)
93326215443944152681699238856266700490715968
264381621468592963895217599993229915608941463
976156518286253697920827223758251185210916864
000000000000000000000000
> (fac 1000)
4023872600 ... < many many other digits>

After reading these first episodes you may be tempted to quit; I am sure the readers who followed me up to this point had this question floating in their minds: is it really worth it?. Probably for most readers the answer is no. But this series is for the most persistent readers, and I hope to show them something positive in the next episode. Keep reading and see you next time!


Merriodoc Brandybuck

Posts: 225
Nickname: brandybuck
Registered: Mar, 2003

Re: The adventures of a Pythonista in Schemeland/4 Posted: Oct 1, 2008 10:45 AM
Reply to this message Reply
Am I the only one getting impatient with the training-wheels approach of this series? I think these first 4 could have been condensed into one post easily.

I've been looking forward to this series since I first saw it. I'm guess by the time we get to 7 we'll get to something good?

If I'm the only one chomping at the bit, that's fine. I just know I dig this stuff but I haven't read anything I didn't pick up in day 1 of my AI class 15 years ago.

That said, I'm anxiously looking forward to the rest.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: The adventures of a Pythonista in Schemeland/4 Posted: Oct 1, 2008 12:03 PM
Reply to this message Reply
> Am I the only one getting impatient with the
> training-wheels approach of this series? I think these
> first 4 could have been condensed into one post easily.

I agree but I've previously put time into learning a little LISP. The problem with everything I've read in the past is that there's always a promise of these great things you can do but all the in-depth examples given are trivial and unimpressive.

I'm hoping this one will be different.

Merriodoc Brandybuck

Posts: 225
Nickname: brandybuck
Registered: Mar, 2003

Re: The adventures of a Pythonista in Schemeland/4 Posted: Oct 1, 2008 12:19 PM
Reply to this message Reply
I think the multitasking engine example here is less than trivial

http://www.scheme.com/tspl3/

Your opinion may vary. I like the book. As is mentioned in the post, it is dense. Then again, perhaps so am I...

John Cowan

Posts: 36
Nickname: johnwcowan
Registered: Jul, 2006

Re: The adventures of a Pythonista in Schemeland/4 Posted: Oct 1, 2008 4:28 PM
Reply to this message Reply
To fix Chicken's numeric support, say "chicken-setup numbers" to the shell, and then put "(use numbers)" at the beginning of your program.

Michele Simionato

Posts: 222
Nickname: micheles
Registered: Jun, 2008

Re: The adventures of a Pythonista in Schemeland/4 Posted: Oct 1, 2008 9:31 PM
Reply to this message Reply
> I'm anxiously looking forward to the rest.

Eh, eh, this was the intent ;-)
Keep in mind that:

1. this will be a long series and the length of the introduction is proportional to the length of the series

2. the impatient can already look forward at the future: it is enough to apply Google translator to the original Italian
articles

3. there will be two other introductory posts (however with code and benchmarks, so not just words), then 6 dense posts about Scheme macros which are definitely far from trivial.

Have faith,

M.S.

Merriodoc Brandybuck

Posts: 225
Nickname: brandybuck
Registered: Mar, 2003

Re: The adventures of a Pythonista in Schemeland/4 Posted: Oct 2, 2008 4:47 AM
Reply to this message Reply
Faith I have. Patience, still working on that one :-)

I've had mixed results with various translators over the years, including Google's, which is why I haven't bothered.

Daniel Weinreb

Posts: 3
Nickname: dlweinreb
Registered: Oct, 2008

Re: The adventures of a Pythonista in Schemeland/4 Posted: Oct 12, 2008 1:37 PM
Reply to this message Reply
While I see the sense in which you're saying that the parentheses are "useless" (indentation could be used instead), that's mainly because this is such a small and simple program. In more serious code, a smaller fraction of the parens are at the beginnings and ends of lines. Also, you pointed out in part 3 reasons that the parens are beneficial.

Just for the record, arbitrary-precision integers are required in Common Lisp and are supported in all implementations.

There isn't a standard way to access command line arguments in Common Lisp either, mainly because Lisp has not traditionally been used that way. There are many libraries around that provide portable ways to deal with operating system functionality, such as "usocket" for Unix-style sockets. There might be one somewhere for "argv", but I don't know.

Grant Rettke

Posts: 23
Nickname: grettke
Registered: Nov, 2008

Re: The adventures of a Pythonista in Schemeland/4 Posted: Nov 2, 2008 1:00 AM
Reply to this message Reply
TSPL is *the* best book for any programmer who wants to learn the Scheme programming language.

Its clarity, conciseness, brevity, and quality of writing make it a joy to read.

It is said that if you grok everything in that book, you truly understand Scheme.

PLAI and SICP are not a good place to learn Scheme.

I'm reading HtDP right now, and it is about learning how to program. It is not a book about learning Scheme, but you will learn Scheme in the process. Probably it is not the right place for "hobbyists" unless they want to learn about Functional Programming and one approach to "how to program".

Grant Rettke

Posts: 23
Nickname: grettke
Registered: Nov, 2008

Re: The adventures of a Pythonista in Schemeland/4 Posted: Nov 2, 2008 1:01 AM
Reply to this message Reply
Your point about time being the real obstacle is dead on.

It simply isn't considered to be normal in the industry to study anything for more than a few weeks.

Welcome to the mad scientist club!

Grant Rettke

Posts: 23
Nickname: grettke
Registered: Nov, 2008

Re: The adventures of a Pythonista in Schemeland/4 Posted: Nov 2, 2008 1:03 AM
Reply to this message Reply
Your disappointment about the lack of a standard 'first' rather than 'car' function made me laugh. I used to think the same thing.

The Scheme community would be more apt to tell you "define it yourself, what is the big deal". And, they are right.

You can redefine car and you know, the world won't end. To your point, do so with standard functions and you get portability right there.

The Scheme community seems to be full confident, self-motivated/directed/driven people that don't worry about being told the right thing to do because they can figure it out for themselves. It is shockingly different from the Perl/Python/Ruby/Java/.NET world!

Grant Rettke

Posts: 23
Nickname: grettke
Registered: Nov, 2008

Re: The adventures of a Pythonista in Schemeland/4 Posted: Nov 2, 2008 1:22 AM
Reply to this message Reply
It is true what they say, after a while you don't really notice the parentheses much at all.

You don't notice the semicolons in Java, C#, C++, or Perl, do you? :)

Flat View: This topic has 11 replies on 1 page
Topic: A new course for the "Adventures of a Pythonista in Schemeland" Previous Topic   Next Topic Topic: The Adventures of a Pythonista in Schemeland/1

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use