0

So I am having a list of lists. Let's say is '('(1 2) '(3 4)) and if I apply car the result is going to be ''(1 2). What could I use in order to obtain '(1 2).

(car '('(1 2) '(3 4))) ''(1 2)

Raluca
  • 3
  • 1
  • 2
    You're double-quoting the nested lists. Try `(car '((1 2) (3 4)))`. – Mulan Mar 23 '19 at 21:09
  • 1
    @user633183 is right. However, if you're new at this, it might be better to avoid quote entirely and just use `list` instead. Like `(car (list (list 1 2) (list 3 4)))`. See [_What is the difference between quote and list?_](https://stackoverflow.com/questions/34984552/what-is-the-difference-between-quote-and-list) for more explanation on this. – Alex Knauth Mar 23 '19 at 21:41

1 Answers1

2

'expression evaluates to expression verbatim whatever expression is. However Racket REPL has a weird visualization that is doesn't really print the result but rather an expression that evaluates to the same result. Thus REPL will print 'expression even though the result was expression. Evaluating the REPL output then always prints the same again.

So imagine you do '(1 2) you get '(1 2) back and make the assumption that that ' somehow is part of the data and try to do '('(1 2) '(3 4)) instead of '((1 2) (3 4)). Now since 'x is reader sugar for (quote x) you will have made '((quote (1 2)) (quote (3 4))) withe quote being just symbols and not code.

If you really want (1 2) from it you need to do car,cdr,car or cadar for short:

(cadar '((quote (1 2)) (quote (3 4)))) 
; ==> (1 2) , but racket will print '(1 2)

If you didn't really want the extra lists but just that the data was ((1 2) (3 4)) you did correct:

(car '((1 2) (3 4))) 
; ==> (1 2) , but racket will print '(1 2)

If you display the value it will actually print the correct result: (display '(1 2)) prints (1 2) and not '(1 2).

There is a setting to turn off the confusion. In the dropdown in the bottom where you can select choose language and under "The racket language" you have options on the right side where you can change "output style" to write. Now it will print (1 2) rather than an expression that evaluates to (1 2) in the racket language too.

Sylwester
  • 47,942
  • 4
  • 47
  • 79