Results 1 to 5 of 5

Thread: Scheme Learning and Q&A, #1

Hybrid View

  1. #1

    Scheme Learning and Q&A, #1

    Scheme, a derivative of LISP (LISt Processing language, or Lots of Irritating Superfluous Parentheses), seems like it would be a syntax strict, extremely picky language with lots of strange functions, commands, etc. *This is a myth. *The R5RS standard specifies what a complient Scheme interpreter/compiler should do (if you do not yet have an interpreter/compiler, check out either DrScheme or MIT Scheme.)

    For these lessons, you will only need an interpreter. *I do not think that either of the ones I mentioned above are better than the other, so make a choice. *If you find another interpretter that is R5RS complient, then by all means, try it out. *If you have problems with DrScheme, I will be able help more with that interpreter than the others as that is the one I am most familiar with. *I will try to help with others, but I will probably not be as confident. *Please post your code so that I and others can first make sure that it is not in the code itself.

    Scheme is case insensitive, so, if you insist, you can write all your code with the caps lock on and not care, but all my code will be in lower case. *The only exception to that rule are strings, but that will be covered later.

    All programming tutorials have the learner first see a program written in that language that says hello world. *Having you learn how to output "Hello World!" seems less important than having you understand lists since the understanding of lists is absolutely essential to learning Scheme (do not worry though, we will get into strings eventually, which are case sensitive.) *Thus, for our first program, I will show a few pieces of code that will create a list containing two symbols: hello and world. *This will be my compromise for not having a "Hello World!" program.
    Code:
    (list 'hello 'world)
    (cons 'hello '(world))
    Both of these s-expressions (a list of symbols) should output the same thing: (hello world). *What are these two s-expressions doing? *The first, with the list function (notice how the function comes first)*, takes a variable number of arguments (notice how the arguments all come after the function)*, and puts them all in a list. *The second, with the cons function, takes two arguments: the first is any datum you would like it to be (it can even be another list) and the second has to be a list (well, technically, it can be anything, but for now, it has to be a list.)

    Notice how there is a single quote before hello, world, and (world). *These are needed since neither hello nor world are previously defined. *If you eliminate the single quotes, your interpreter will complain. *Also, note that the single quote tells Scheme that the symbol immediately after the quote or symbols within the list that the quote prepends will not be interpreted as Scheme functions. *So take the following for example:
    Code:
    (list 'list 'hello 'world)
    (cons 'cons '(hello world))
    Now you have these lists, but you need to get at certain parts of the list. *To start out, an examination of car and cdr (pronounced 'cudder'), leftovers from LISP. *Both car and cdr take one argument, a list. *Car will take the list and return you the first element in that list. *Cdr will take the list and return you the list without the car.
    Code:
    (car '(h e l l o))
    (cdr '(h e l l o))
    The first s-expression will return the symbol h. *The second will return a list, (e l l o).

    If you do not understand prefix notation yet, then keep reading. *If you do, then you can look at the problems I have provided.

    In math, the norm is to use infix notation. *The add function in 4 + 5 is fixed inside our mathematical expression. *With prefix notation, that same expression would look like (+ 4 5). *To tie it into some of the rest of this lesson, the function is always the car of a list, and its arguments are the cdr of the list.

    Problems

    1. What does the following return?
    (car (cdr '(h e l l o)))

    2. How would you return the second l in the list from the previous question?
    3. Take the following list: (a b (c d) e f). *Figure out a way to return c, d, and finally f. *Note: I am not asking you to return a list with c, d, and f. *Just use three different s-expressions to return each of these symbols.
    4. The list function is nothing more than syntactic sugar (i.e. it is not needed as other constructs can do the same thing, but it may not be as pretty.) *How could you construct a list containing the symbols foo, bar, and baz without using the list function (using what we have learnt thusfar)?


    * Scheme uses prefix notation. *This means that the function you want to use must come first before its arguments. *If you want to perform another function on some other data, then you need another list. *So, if you wanted to cons 'a on to (list 'b 'c), the s-expression would look like: (cons 'a (list 'b 'c)).

  2. #2

    Re: Scheme Learning and Q&A, #1

    To avoid the problem of parenthesis matching, I would suggest using either Emacs (what I use and highly recommend) or VIM. *Since Emacs was programmed in LISP, it naturally has nicer support for Scheme. *I will include a portion of my .emacs that makes Emacs run nicely with DrScheme, and make syntax highlighting a little nicer.
    Code:
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;; The customization below is the best way to run Scheme locally.
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    ;;; Files mentioned below are found in "~/emacs" and "/usr/unsup/emacs".
    ;;; (If you're working at home, you may need to copy some files to a
    ;;; directory you call "/usr/unsup/emacs" or change the following.)
    ;;;
    (setq load-path (cons "~/emacs" load-path))
    
    ;;; Scheme program mode
    
    ;;; Tell emacs that files that end in .ss are Chez Scheme files.
    ;;; There already is a mapping for the suffix .scm.
    ;;;
    (add-to-list 'auto-mode-alist '("\\.ss$" . scheme-mode))
    (add-to-list 'auto-mode-alist '("\\.def$" . scheme-mode))
    (add-to-list 'auto-mode-alist '("\\.tst$" . scheme-mode))
    
    ;;; use cmuscheme instead of the standard scheme mode.
    ;;;
    (autoload 'run-scheme "cmuscheme" "Run Scheme in an emacs buffer." t)
    (autoload 'scheme-mode
     * * * *"cmuscheme"
     * * * *"Mode for Scheme programming and interaction"
     * * * *t)
    
    ;;; Special behavior that I want for scheme program files
    ;;;
    (setq scheme-mit-dialect nil)
    
    ;;; The following uses "drscheme-jr" as the scheme interpreter
    ;;;
    (setq scheme-program-name "drscheme-jr")
    ;;; To use scm342 as the scheme interpreter uncomment the following
    ; (setq scheme-program-name "scm342")
    
    ;;; Customize scheme mode
    ;;;
    (add-hook
     'scheme-mode-hook
     (function
     *(lambda ()
     * *(define-key scheme-mode-map "\n" 'newline)
     * *(define-key scheme-mode-map "\r" 'newline-and-indent)
     * *(put 'cases 'scheme-indent-function 1)
    
     * *(if window-system
     * * *(progn
     * * * *;; turn on fancy coloring of scheme programs (optional)
     * * * *(font-lock-mode)
    
     * * * *;; Modify the font-locked keywords to include some used in EOPL
     * * * *;; (The following is for GNU emacs 20)
     * * * *(if (> emacs-major-version 19)
     * * * * * *(progn
     * * * * * *(setq font-lock-keywords
     * * * * * * * * *(cons 't
     * * * * * * * * * * *(cons
     * * * * * * * * * * * '("\\(trustme!\\|(\\(->\\|cases\\|isa\\|forall\\|def\\(rep\\|type\\|ine-
    datatype\\)\\|public\\|has-type\\(\\|-trusted\\)\\)\\>\\)"
     * * * * * * * * * * * * (1 font-lock-keyword-face))
     * * * * * * * * * * * (cdr font-lock-keywords))))
     * * * * * *(font-lock-fontify-buffer)))
     * * * *))
    
     * *(fset 'untyped
     * * * *(lambda ()
     * * * * *(interactive)
     * * * * *(setq scheme-program-name "drscheme-jruntyped")))
     * *(fset 'typed
     * * * *(lambda ()
     * * * * *(interactive)
     * * * * *(setq scheme-program-name "drscheme-jrtyped")))
     * *)))
    
    ;;; Portable scheme debugger mode
    ;;;
    (autoload 'psd-mode "psd" "Portable Scheme Debugger mode" t nil)
    ;;;
    ;;; uncomment the following to turn on psd-mode by default when running Scheme
    ;(add-hook 'inferior-scheme-mode-hook
    ; * * * *(function (lambda () (psd-mode 1))))
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;; End of Scheme customizations
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    A few tips: M-x run-scheme will run the Scheme interpreter. C-c C-e will send the s-expression that the cursor is on to the Scheme interpreter. C-c C-r will execute a region.

  3. #3
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re: Scheme Learning and Q&A, #1

    Since I havn't gotten my Scheme notes with me, this is from the top of my head..
    1. What does the following return?
    (car (cdr '(h e l l o)))
    e
    2. How would you return the second l in the list from the previous question?
    (car (cdddr '(h e l l o)))
    3. Take the following list: (a b (c d) e f). *Figure out a way to return c, d, and finally f. *Note: I am not asking you to return a list with c, d, and f. *Just use three different s-expressions to return each of these symbols.
    Lets call the list L, just for convenience.

    To get 'c':
    (car (cddr L))
    To get 'd':
    (car (cdr (cddr L))
    To get 'f':
    (car (cddddr L))
    4. The list function is nothing more than syntactic sugar (i.e. it is not needed as other constructs can do the same thing, but it may not be as pretty.) *How could you construct a list containing the symbols foo, bar, and baz without using the list function (using what we have learnt thusfar)?
    (cons '(f o o) *'(b a r) '(b a z))

    Whats the next lesson? Learning the true differencies in ( ', ` ) ?

  4. #4
    Guest

    Re: Scheme Learning and Q&A, #1

    Sans-hubris and others

    I trust that this will be rewritten and made into a PET. Beg, plead, bribe... etc.

    btw.

    std::cout << "cool tutorial"

    Wow, scheme uses alot of '( )'s

  5. #5

    Re: Scheme Learning and Q&A, #1

    Sans-hubris and others

    I trust that this will be rewritten and made into a PET. Beg, plead, bribe... etc.

    btw.

    std::cout << "cool tutorial"

    Wow, scheme uses alot of '( )'s
    Here is what I would like to happen. *I would like to get through all these lessons with all questions and corrections made. *This way, when I do make a PET, it'll be as clear and concise as possible, but for this to happen, I need input.

Similar Threads

  1. Who wants to learn Scheme?
    By ndogg in forum Linux - Software, Applications & Programming
    Replies: 17
    Last Post: 04-12-2002, 01:39 AM
  2. Learning php
    By Vagrant in forum Linux - Software, Applications & Programming
    Replies: 3
    Last Post: 03-20-2002, 12:46 AM
  3. learning php
    By philosophia in forum Linux - Software, Applications & Programming
    Replies: 9
    Last Post: 03-01-2002, 08:25 PM
  4. Color Scheme
    By jman_77 in forum Announcements and Suggestions
    Replies: 2
    Last Post: 08-16-2001, 03:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •