[racket] please help to change vector to list

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Fri Jun 3 14:19:16 EDT 2011

On Fri, Jun 3, 2011 at 1:51 PM, Yingjian Ma <yingjian.ma1955 at gmail.com> wrote:
> Thank you Danny.  I used to program in Visual Basic and then Java with
> related stuff, such as HTML, Javascripts.  I learned C and Pascal in
> school.  Racket is quite different.  It seems I need to use recursion much
> more.


Cool.  You can program Racket as if it were Java or JavaScript.  Just
as in those language, functions in Racket will return values to their
callers.  Furthermore, they can have side effects, like printing to
standard output or opening files.


Your earlier question about how to get two things to display from a
function has an answer that you already know from Java: to observe a
value as you're running through a function, you can use a side effect,
such as printing.

    (define (say-hello name)
       (printf "hi ~s\n" name)
       (printf "I'm going to compute your doubled name.")
       (string-append name name))

The equivalent Java code for this is:

    public static String sayHello(String name) {
        System.out.print("hi " + name + "\n");
        System.out.print("I'm going to compute your doubled name.");
        return name + name;
    }

That is, since Racket functions always "return", there's no
distinguished keyword built-in to the language.  If you have a
sequence of things, the last one's the value that's going to be
ultimately returned to the caller.  But functions in Racket act much
like functions in the other languages that you've experienced: they
compute values, and can perform side effects during their evaluation.



One reason why we like functions that return values is because unit
testing is fairly easy with them.  For example:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket/base
(require rackunit)

;; computes square
(define (sq x)
  (* x x))

;; computes hypotenuse
(define (hypo a b)
  (sqrt (+ (sq a) (sq b))))

(check-equal? (hypo 3 4) 5)
(check-equal? (hypo 1 1) (sqrt 2))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


If hypo were printing, rather than returning, then it would become
harder to test.




Although you can process a list through recursion, where the functions
tend to look like this:

    (define (process-list l)
       (cond
          [(empty? l)
           ...]
          [else
           ... (first l) ... (process-list (rest l)) ...]))

you don't have to use recursion.  Alternatively, you can use 'for'
loops if you're comfortable with those.  For example:

    (for ([x '(a b c d e f gh)])
      (printf "Hey, I see: ~s\n" x))

The same loops work across different sequence types.  For example:

   (for ([x (in-range 10)])
     (displayln 10))

   (for ([x (current-command-line-arguments)])
     (displayln x))

With the experience you have, you definitely should look at the Racket Guide!

     http://docs.racket-lang.org/guide/index.html.

In particular, you can see: http://docs.racket-lang.org/guide/for.html
for more information about Racket's version of 'for' loops.



Posted on the users mailing list.