1
votes

I'm just picking up on some Scheme for the first time today and I wanted to try and write a few functions that would give me an idea of the basic syntax but for some reason (maybe because of my lack of sleep) it's proving much more difficult than I expected.

first, I want to make a function that displays each item in a list. Essentially I'd give it:

(mydisplay '(3 4 "dog" 9)) and it would display those items.

Next I'd like to write the an equivalent of this C++ snipet in Scheme:

for (int i = 6; i < 25; i+=6)
{
  cout << i << endl;
}

Any tips or help would be greatly appreciated! Sorry for the possibly waaaayyy too simple questions but I just can't seem to grasp some of the syntax

Thanks!

3

3 Answers

1
votes

When I looked at Lisp for the first time I knew 6 languages and had experienced that learning a new language was very simple. Unfortunately the experience I had wasn't really how to learn a new language since when I thought I knew 6 languages the truth is that I knew 6 dialects springing from a common ancestor called Algol. Perl, Java, C++ are almost the same language and if you know one you know how to do the other within a day.

You need to find yourself a nice book or resource that you can follow and learn Scheme as if you have never ever known a programming language before. You can have a look at the Rosetta Code for Scheme to compare how some algorithms are done in Scheme but even if you see the tasks have similar structure they tend to do quite different things.

;; roll your own using recursion
(define (my-display lst)
  (when (pair? lst)
    (display (car lst))
    (newline)
    (my-display (cdr lst))))

;; one with higher order procedures
(define (my-display lst)
  (for-each (lambda (e) 
              (display e)
              (newline))
            lst))


;; Close to your algol for loop 
(let make-number ((count 6))
  (if (< count 25)
      (begin
        (display count)
        (newline)
        (make-number (+ count 6)))
      'return-value))

;; A better approach 
;; make a list of the numbers based on start end and step
(define (make-range start end step)
  (let rec ((cnt start))
    (if (> cnt end)
        '()
        (cons cnt (rec (+ cnt step))))))

(my-display (make-range 6 25 6)) ; prints the numbers

;; one with higher order procedures
;; uses SRFI-1 List Librarys unfold instead of named let
(define (make-range start end step)
  (unfold (lambda (x) (< end x))
          values
          (lambda (x) (+ x step))
          start))

I do not recommend you to use the do loop. Use named let or higher order procedures and keep the printing part outside.

I really enjoyed the SICP video lectures. They are a tad old and thus are a older version of Scheme than today, but someone has made support for it in racket. When you know one lisp language it's easy to learn a different dialect, especially a later Scheme report.

0
votes
> (define (mydisplay xs)
    (for-each (lambda (x) (display x) (newline)) xs))
> (mydisplay '(3 4 "dog" 9))
3
4
dog
9
> (do ((i 6 (+ i 6)))
      ((not (< i 25)))
    (display i) (newline))
6
12
18
24
0
votes

As you will find out, there are many ways to loop in Scheme. I would start with the most fundamental, namely tail recursive calls.

for (int i = 6; i < 25; i+=6)
{
  cout << i << endl;
}

(let loop ([i 6])
  (if (< i 25) 
      (begin
        (display i)
        (newline)
        (loop (+ i 6)))))

The construct used above is called a named let and you will see it over and over again in Scheme code.

Note that one-armed if is used above. If your Scheme implementation has when it becomes:

(let loop ([i 6])
  (when (< i 25) 
    (display i)
    (newline)
    (loop (+ i 6))))