Page 3 - Discuss Scheme in the Other Programming Languages forum on Dev Shed. Scheme A place for discussing programming languages not covered in specific forums such as Assembler, COBOL, etc. - you get the idea.
Ok I know what lambda( ) is ( Python ) but.. what is it doing there in scheme? How does that part work... even though in my tutorial i am in that part.. but that code is a bit different.
Posts: 596
Time spent in forums: 2 Days 3 h 56 m 23 sec
Reputation Power: 22
Quote:
Originally Posted by xlordt
Ok I want to know if there is a better shorter way of doing this?
Code:
(define age 27 )
(define name "xlordt")
( if (< age 18)
(begin
(display "Your not allowed to view this content") (newline)
)
(begin
(set! name "The King!!")
(display name) (newline)
)
)
Also how many scheme programmers are here at dev? Can somene show me a program created in scheme?
one thing that may seem trivial but which is actually really important is indentation and layout - you should use and get used to the idiomatic style because everyone else's code will be written that way, and you need to be able to read it properly, just as you need other people to read your code easily. So never put a paren on it's own line and always line up code that's in the same scope. Again, this would be handled for you by a good editor
Code:
(define age 27)
(define name "xlordt")
(if (< age 18)
(begin
(display "Your not allowed to view this content") (newline))
(begin
(set! name "The King!!")
(display name) (newline)))
You will realise the importance of this if you end up posting code on comp.lang.scheme sometime. That's probably also the best place to start looking at other people's code, from fragments to whole apps.
Posts: 596
Time spent in forums: 2 Days 3 h 56 m 23 sec
Reputation Power: 22
Quote:
Originally Posted by xlordt
Ok I know what lambda( ) is ( Python ) but.. what is it doing there in scheme? How does that part work... even though in my tutorial i am in that part.. but that code is a bit different.
both functions (deliberately) contain infinite loops - if you go to the parent dir there are infinite loops in scores of languages, for some reason...
Scheme's/any lisps's lambda is much more powerful than the python construct, in that it creates first class objects (which you already know are anonymous functions) and isn't limited to one line or whatever. The way scope/lexical closure is treated is more powerful too.
As for the great editor debate, while I am usually an Emacs partisan, in this case I would recommend Dr Scheme, which comes with one of the best language-specific IDEs I know of, and has support for a number of variants of the language (though in most cases, you would want it set to 'Standard (R5RS)', unless you were studying from a book that uses the different versions, like How To Design Programs does). The tutorial 'Learning to Program with DrScheme' covers both the language itself and how to use the IDE (though it is a bit dated now). The interpreter and IDE are open source, and there pre-compiled installers available for Windows, FreeBSD, Mac, and certain some of the more common Linux distros.
As for programs in Scheme, well, I could point to my own Hex dump example, but that's hardly an example of good code. If you do a search on 'Scheme' here in DevShed, you'll find some previous threads, like these:
FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
Last edited by Schol-R-LEA : June 8th, 2006 at 01:30 PM.
Posts: 1,785
Time spent in forums: 1 Month 2 Weeks 2 Days 18 h 21 m 8 sec
Reputation Power: 1569
One more page to add: Scheme Idioms on Ward's Wiki. Actually, there are quite a few wiki pages on Scheme there, though not all of the are favorable, to say the least
Last edited by Schol-R-LEA : June 8th, 2006 at 01:58 PM.
Posts: 5,573
Time spent in forums: 1 Month 11 h 9 m 33 sec
Reputation Power: 406
Quote:
Originally Posted by Schol-R-LEA
One more page to add: Scheme Idioms on Ward's Wiki. Actually, there are quite a few wiki pages on Scheme there, though not all of the are favorable, to say the least
btw.. for a minute there i could not access the forum.. it was loading slow.. so i memod you saying thanx for DR Scheme.
Posts: 1,785
Time spent in forums: 1 Month 2 Weeks 2 Days 18 h 21 m 8 sec
Reputation Power: 1569
You might also want to see what is said on Ward's Wiki about the lambda function and lambda calculus in general. To make a long story short, lambda is a function for creating functions; when you write something like
Scheme Code:
Original
- Scheme Code
(define ++
(lambda (x)
(+ 1 x)))
What it is really doing is generating a function that takes an argument x and applies the + function to x and 1, then binds the function to the variable name ++.
(And yes, you can use ++ as a function or variable name, if you really wanted to, though it's not generally advised.)
The form
Scheme Code:
Original
- Scheme Code
(define (++ x)
(+ 1 x))
Is really just a shortcut way of doing the same thing (it actually is a macro, which replaces that with the first example).
Scheme (and the other Lisp languages) take a very different approach towards programming than languages like C or Java, and it can take a bit of getting used to.
Last edited by Schol-R-LEA : June 8th, 2006 at 02:01 PM.
Posts: 5,573
Time spent in forums: 1 Month 11 h 9 m 33 sec
Reputation Power: 406
How would i retrive user inputs? Was meaning to ask this before.. forgot, Im just still a little curious, even though I'm learning as I go. Also thanx again.
Posts: 1,785
Time spent in forums: 1 Month 2 Weeks 2 Days 18 h 21 m 8 sec
Reputation Power: 1569
The short answer is, (read) and (read-char). However, that's just the starting point.
In the R5RS Scheme, stream I/O is handled through the port abstraction. To open a port, you would use
(open-input-file filename)
or
(open-input-file filename)
as appropriate, which would return a port handle for the named file. To close the port when finished, you would call
(close-input-port port)
or
(close-input-port port)
as appropriate. Alternately, you could call use
(call-with-input-file stringproc)
or
(call-with-output-file stringproc)
Which opens the port, calls proc with the port object as it's argument, and then closes the port when the called function returns.
The port arguments in these are optional; If you call these with no with no port argument, they asume the default ports (the equivalent of stdin and stdout for C). If you call a function using (with-input-from-file) or (with-output-to-file), it has the effect of redirecting the default ports to the one given as the filename argument.
(read-char) reads exactly one character from input; if more than one character was input, then it will continue to read the enter characters until the buffer is empty or has been flushed somehow. (peek-char) reads a single character without removing it from the input buffer; it is used for lookahead in parsing and similar roles.
(read) gets a single Scheme expression, whether it is a symbol, a string, a number, or a list; thus, if you have a function
Scheme Code:
Original
- Scheme Code
(define (get-name)
(display "What is your name?")
(read))
then if you enter John Jacob Jingleheimer Schmitt, it will read in John as a symbol (not as a text string), and each subsequent call would read opne of the names as a single symbol until the buffer is empty. On the other hand, if you were to enter (John Jacob Jingleheimer Schmitt), it would read the whole input text as a single list, and if you entered "John Jacob Jingleheimer Schmitt", it would treat it as a single text string.
Not surprisingly, (write-char) writes out a single character. (display) and (write) work similarly, except that where (display) outputs the value of the expression or symbol, (write) outputs the Scheme representation of it; e.g., if you printed a string using (display), it would be shown without the double-quotes, while (write) would show the quotes; (display) would output the character literal #\a, it would print the character a, while (write) would print the character literal as it appears in the code, #\a; and so on.
The predicates (input-port?) and (output-port?) test the argument and returns #f if it is anything other than a port of the approriate type. (eof-object?) returns #t if the port in question has reached the end-of-file marker, #t if it hasn't. (char-ready? [port]) returns #t if there is a character in the port buffer ready to be read, #f otherwise.
This is the just the basic I/O that's built into the language; there are several libraries which expand on these. The following SRFIs cover I/O in one manner or another:
(Note that despite it's title, SRFI-40 is not about I/O; the 'streams' in question are a rather different abstraction).
The SLIB library also define several I/O functions (many of which overlap the SRFIs), and the Scheme Cookbook has several useful functions and patterns for file I/O as well.
Last edited by Schol-R-LEA : June 8th, 2006 at 09:34 PM.