cmscalzo
2019-10-29 08:50:17

hello again - what’s the cost of using things like first, second, third, etc.? do they scan the list linearly?


soegaard2
2019-10-29 10:35:00

They are all O(1).


soegaard2
2019-10-29 10:35:54

(second xs) is the same as (first (rest xs))


soegaard2
2019-10-29 10:36:19

and (third xs) is the same as (first (rest (rest xs)))


cmscalzo
2019-10-29 10:55:59

thanks @soegaard2, I know they are, but are they literally like (first (rest (rest xs)))) - as in, following the car pointer, then following the new car pointer, etc.?


cmscalzo
2019-10-29 10:56:23

or does the implementation build pointers to point directly to the second item, the third, etc.?


soegaard2
2019-10-29 10:58:32

Let’s for second assume that (second xs) literally is (first (rest xs)). Then we don’t need to know the underlying representation. We can simply say: time[second] = time[first]+time[rest]=O(1)+O(1)=O(1).


soegaard2
2019-10-29 10:59:59

If we think in terms of the underlying representation in memory, then a pair consists of a type tag, a pointer to the first element and a pointer to the next pair.


soegaard2
2019-10-29 11:00:30

(first xs) simply returns the first pointer in a pair and rest returns the second pointer in a pair.


soegaard2
2019-10-29 11:02:04

This is slightly simplified (some values like booleans and fixnums are usually stored directly in a pair without needing a pointer instead of as a pointer to the value).


cmscalzo
2019-10-29 11:02:56

yes, so the implementation doesn’t keep a pointer to ‘second’, another one to ‘third’ etc., right?


soegaard2
2019-10-29 11:03:16

right


soegaard2
2019-10-29 11:03:52

If you need the last element of a list, you will need to follow all the next pointers and that will take time O(n), where n is the length of the list.


cmscalzo
2019-10-29 11:05:32

ok thanks - I guess it’s a similar thing for split-at?


cmscalzo
2019-10-29 11:05:45

in that case, the cost being O(n)


soegaard2
2019-10-29 11:06:06

Yes.


soegaard2
2019-10-29 11:06:45

The only surprising (if you are used to Scheme) fact is that list? runs in time O(1).


cmscalzo
2019-10-29 11:12:49

great, thank you!


cmscalzo
2019-10-29 14:07:19

hello, how do I plot a line from a list of points? say I have something like ’((1 2) (2 3) (3 4)) - I want to plot a line that connects these points


soegaard2
2019-10-29 14:18:32
#lang racket
(require metapict)

(set-curve-pict-size 400 400)
(defv (xmin xmax) (values -2 10))
(defv (ymin ymax) (values -2 10))

(with-window (window xmin xmax ymin ymax)
  (ahlength (px 10))
  (draw (curve (pt 1 2) -- (pt 2 3) -- (pt 3 4))
        (draw-arrow (curve (pt xmin 0) -- (pt xmax 0)))
        (draw-arrow (curve (pt 0 ymin) -- (pt 0 ymax)))))

soegaard2
2019-10-29 14:19:07

cmscalzo
2019-10-29 14:25:57

ah, very nice, thank you - can I pass the list of points as an argument?


cmscalzo
2019-10-29 14:26:38

(and perhaps add numbers to the axes too?)


soegaard2
2019-10-29 14:28:38
(define (lines xs)
  (curve* (add-between (map (λ(p) (apply pt p)) xs) --)))

(with-window (window xmin xmax ymin ymax)
  (ahlength (px 10))
  (draw (lines '((1 2) (2 3) (3 4)))
        (draw-arrow (curve (pt xmin 0) -- (pt xmax 0)))
        (draw-arrow (curve (pt 0 ymin) -- (pt 0 ymax)))))

soegaard2
2019-10-29 14:32:06

cmscalzo
2019-10-29 14:38:03

thank you!


artemchernyak
2019-10-29 15:16:14

I am getting comfortable with the base racket syntax, but I always learn best by reading other well structured code.

Could any one recomand any idiomatic open source projects which aren’t huge and are fairly self contained?



soegaard2
2019-10-29 15:18:10

Also checkout the blog posts on http://racket-stories.com\|racket-stories.com


soegaard2
2019-10-29 15:19:32

Flappy Bird is also available (the source becomes readable when you open it in DrRacket): https://github.com/soegaard/flappy-bird/blob/master/main.rkt


artemchernyak
2019-10-29 15:27:29

Thanks! Those look like fun projects to explore. I also had no idea about http://racket-stories.com\|racket-stories.com definitely going to keep up with it :slightly_smiling_face:


soegaard2
2019-10-29 15:28:27

If you find interesting (old or new) blog posts on Racket, please submit them to Racket Stories.


artemchernyak
2019-10-29 15:30:19

will do!