IIRC I did one of the challenges in Scheme...
Maybe you need some sleep?
im am assuming i cannot use assembler like i asked and that it was a stupid question too
but is OCaml valid?
It can be considered valid if you do not use an imperative style,and if you use tail recursion.but is OCaml valid?
I only started learning scheme about half an hour ago, but here is my attempt that I have come up with in that time. Constructive criticism is required, as I'm sure I have done a few silly things in there.
Code:;99 bottles of beer song in Scheme (begin (define beer (lambda (bottlesLeft) (if (>= bottlesLeft 1) (begin (display bottlesLeft) (display " bottle(s) of beer on the wall, ") (display bottlesLeft) (display " bottle(s) of beer, take one down, pass it around, ") (display (- bottlesLeft 1)) (display " bottle(s) of beer on the wall.") (newline) (newline) (beer (- bottlesLeft 1))) (begin (display "Go to the store, buy some more, lots of ") (display "bottles of beer on the wall"))))) (beer 100))
0. (define xx (lambda (yy) ...)) is usually shorthanded as (define (xx yy) (...)). Tutorials emphasize the first form to make you see that code = data, and that defining a function is just give a name to a lambda. But once you know that, you can use the syntactic sugar form, which is more widespread.
1. In MIT/GNU Scheme, there's an procedure called (format) that allows you to print formatted output. Look at your implementation's documentation if there's something like that available for you.
2. (begin) is evil. First, it breaks functional programming, as the return value of a (begin ...) is undefined; also, because usually it's a sign that your code should be broken into procedures. Of course, sometimes it simplifies stuff, but at the costs described above. Also, notice that the first (begin) is unnecessary: your interpreter will execute sequentially whatever you give to it... be it 100 procedures or a single one.
3. Now, a philosophical observation. When developing in Lisp, you usually just define procedures that afterwards are executed by a user in the interpreter... in other words, in Lisp you program by creating a new language built "on Lisp" (in contrast to "built in Lisp") and therefore, it transforms itself into a new Lisp dialect specific to your task (Do you want to write a programming language? With Lisp, just do it!). And the Lisp interpreter you use will "automagically" be an interpreter for your language, and also the interface where the user, using your language, can perform some task (or write a script for it).
Of course, as you've experienced, Lisp also allows you to write stuff in a "conventional way"... Notice that Lisp's power is not related with functional programming, but to the "code = data = code" principle.
I *highly* recommend you to watch the MIT SICP videos at: http://groups.csail.mit.edu/mac/clas...sman-lectures/ And, of course, also look at the book (it's available free in the web). Even if the videos were recorded in 1986, they're pretty good to learn Scheme (and also some other CompSci cool stuff).
Last edited by nvteighen; September 27th, 2008 at 01:36 PM.