Here is some code you can type at the guile>, prompt to see some of the Scheme data types at work (mostly lists and vectors). I have inserted brief comments before each line of code explaining what happens. ,, make a list and bind it to the symbol ls guile>, (define ls (list 1 2 3 4 5 6 7)) =>, ,, display the list guile>, ls =>, (1 2 3 4 5 6 7) ,, ask if ls is a vector, #f means it is not guile>, (vector? ls) =>, #f ,, ask if ls is a list, #t means it is guile>, (list? ls) =>, #t ,, ask for the length of ls guile>, (length ls) =>, 7 ,, pick out the first element of the list guile>, (car ls) =>, 1 ,, pick the rest of the list without the first element guile>, (cdr ls) =>, (2 3 4 5 6 7) ,, this should pick out the 3rd element of the list guile>, (car (cdr (cdr ls))) =>, 3 ,, a shorthand for doing the same thing guile>, (caddr ls) =>, 3 ,, append the given list onto ls, print the result ,, NOTE: the original list ls is not modified guile>, (append ls (list 8 9 10)) =>, (1 2 3 4 5 6 7 8 9 10) guile>, (reverse ls) =>, (10 9 8 7 6 5 4 3 2 1) ,, ask if 12 is in the list --- it obviously is not guile>, (memq 12 ls) =>, #f ,, ask if 4 is in the list --- returns the list from 4 on. ,, Notice that the result will behave as true in conditionals guile>, (memq 4 ls) =>, (4 5 6 7) ,, an if statement using the aforementioned result guile>, (if (memq 4 ls) (display 'hey, it's true!n') (display 'dude, it's falsen')) @print{hey, it's true!} =>, guile>, (if (memq 12 ls) (display 'hey, it's true!n') (display 'dude, it's falsen')) @print{dude, it's false} =>, guile>, (memq 4 (reverse ls)) =>, (4 3 2 1) ,, make a smaller list ls2 to work with guile>, (define ls2 (list 2 3 4)) ,, make a list in which the function sin has been ,, applied to all elements of ls2 guile>, (map sin ls2) =>, (0.909297426825682 0.141120008059867 -0.756802495307928) ,, make a list in which the squaring function has been ,, applied to all elements of ls guile>, (map (lambda (n) (expt n n)) ls) =>, (1 4 27 256 3125 46656 823543) ,, make a vector and bind it to the symbol v guile>, (define v #(1 2 3 4 5 6 7)) guile>, v =>, #(1 2 3 4 5 6 7) guile>, (vector? v) =>, #t guile>, (list? v) =>, #f guile>, (vector-length v) =>, 7 ,, vector-ref allows you to pick out elements by index guile>, (vector-ref v 2) =>, 3 ,, play around with the vector: make it into a list, reverse ,, the list, go back to a vector and take the second element guile>, (vector-ref (list->,vector (reverse (vector->,list v))) 2) =>, 5 ,, this demonstrates that the entries in a vector do not have ,, to be of uniform type guile>, (vector-set! v 4 'hi there') =>, 'hi there' guile>, v =>, #(1 2 3 4 'hi there' 6 7) Source.


Яндекс.Метрика Рейтинг@Mail.ru Free Web Counter
page counter
Last Modified: April 23, 2016 @ 2:04 am