0
votes

Very new to Scheme, so I'm sorry for a basic question. Whenever I print out something as a test, my result always contains the word "list" in the list that is printed.

My code:

(define get-lower-half
  (lambda (lst n)
    (if (< n (quotient (length lst) 2))
      (cons (list-ref lst n) (get-lower-half lst (+ n 1)))
      '())))
(get-lower-half '(1 2 3 4 5) 0)

My result is then:

(list 1 2)

instead of just

(1 2)

The examples of using cons I find online don't have this problem, what the heck am I doing wrong here? I'm using DrRacket as my IDE with Intermediate Student with Lambda as the language.

2
That's not part of the list, that's just how the Racket REPL displays lists.Barmar

2 Answers

2
votes

I'm pretty sure you're expecting '(1 2) (a list), and not (1 2).

(1 2) is not a valid procedure in Racket or in the intermediate language.

In the intermediate language, lists are represented with the list procedure, (list 1 2) and not '(1 2) like in regular Racket. What you are seeing is regular intermediate language behaviour.

0
votes

In Scheme there are the concepts of datum and datum syntax.

A datum is the mathematical part of an object. For example, the number 3 or the pair (x y) is a datum. A datum syntax is a way to represent datum. For example, #x03 or 3 or #b11 are datum syntax for representing the datum 3. In the same way, the datum (x y) can be represented in syntax as (x y) or (x . (y . ())).

The scheme output has many options to print the datum. Some output functions print the datum such that what they print to be valid code that creates the given datum or they can print the datum as valid syntax as data that will create that datum.

See io-ports and datum definitions.