mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-11-27 04:44:08 +03:00
added struct, quote
This commit is contained in:
parent
930d12e98f
commit
150b232b9b
@ -11,12 +11,6 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
```racket
|
```racket
|
||||||
#lang racket ; defines the language we are using
|
#lang racket ; defines the language we are using
|
||||||
|
|
||||||
; TODO:
|
|
||||||
; quote
|
|
||||||
; structs
|
|
||||||
; control flow (pattern-matching, loops, sequences)
|
|
||||||
|
|
||||||
|
|
||||||
; Single line comments start with a semicolon
|
; Single line comments start with a semicolon
|
||||||
#| Multiline strings can be written
|
#| Multiline strings can be written
|
||||||
using three "'s, and are often used
|
using three "'s, and are often used
|
||||||
@ -27,7 +21,7 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
;; 1. Primitive Datatypes and Operators
|
;; 1. Primitive Datatypes and Operators
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Numbers
|
;;; Numbers
|
||||||
9999999999999999999999 ; integers
|
9999999999999999999999 ; integers
|
||||||
3.14 ; reals
|
3.14 ; reals
|
||||||
6.02e+23
|
6.02e+23
|
||||||
@ -36,8 +30,10 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
|
|
||||||
; Function application is written (f x y z ...)
|
; Function application is written (f x y z ...)
|
||||||
; where f is a function and x, y, z, ... are operands
|
; where f is a function and x, y, z, ... are operands
|
||||||
|
; If you want to create a literal list of data, use ' to stop it from
|
||||||
; Here are a few arithmetic operators
|
; being evaluated
|
||||||
|
'(+ 1 2) ; => (+ 1 2)
|
||||||
|
; Now, some arithmetic operations
|
||||||
(+ 1 1) ; => 2
|
(+ 1 1) ; => 2
|
||||||
(- 8 1) ; => 7
|
(- 8 1) ; => 7
|
||||||
(* 10 2) ; => 20
|
(* 10 2) ; => 20
|
||||||
@ -48,7 +44,7 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
(exact->inexact 1/3) ; => 0.3333333333333333
|
(exact->inexact 1/3) ; => 0.3333333333333333
|
||||||
(+ 1+2i 2-3i) ; => 3-1i
|
(+ 1+2i 2-3i) ; => 3-1i
|
||||||
|
|
||||||
; Booleans
|
;;; Booleans
|
||||||
#t ; for true
|
#t ; for true
|
||||||
#f ; for false
|
#f ; for false
|
||||||
(not #t) ; => #f
|
(not #t) ; => #f
|
||||||
@ -57,12 +53,12 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
(= 1 1.0) ; => #t
|
(= 1 1.0) ; => #t
|
||||||
(= 2 1) ; => #f
|
(= 2 1) ; => #f
|
||||||
|
|
||||||
; Characters
|
;;; Characters
|
||||||
#\A ; => #\A
|
#\A ; => #\A
|
||||||
#\λ ; => #\λ
|
#\λ ; => #\λ
|
||||||
#\u03BB ; => #\λ
|
#\u03BB ; => #\λ
|
||||||
|
|
||||||
; Strings are fixed-length array of characters.
|
;;; Strings are fixed-length array of characters.
|
||||||
"Hello, world!"
|
"Hello, world!"
|
||||||
"Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character
|
"Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character
|
||||||
"λx:(μα.α→α).xx" ; any Unicode character can appear in a string constant
|
"λx:(μα.α→α).xx" ; any Unicode character can appear in a string constant
|
||||||
@ -83,7 +79,7 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
;; 2. Variables
|
;; 2. Variables
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
; You can create a variable using define
|
; You can create a variable using define
|
||||||
; a variable name can use any characters except: () [] {} " , ' ` ; # | \
|
; a variable name can use any character except: ()[]{}",'`;#|\
|
||||||
(define some-var 5)
|
(define some-var 5)
|
||||||
some-var ; => 5
|
some-var ; => 5
|
||||||
|
|
||||||
@ -100,14 +96,29 @@ some-var ; => 6
|
|||||||
me) ; => "Bob"
|
me) ; => "Bob"
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 3. Collections
|
;; 3. Structs and Collections
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
; Structs
|
||||||
|
(struct dog (name breed age))
|
||||||
|
(define my-pet
|
||||||
|
(dog "lassie" "collie" 5))
|
||||||
|
my-pet ; => #<dog>
|
||||||
|
(dog? my-pet) ; => #t
|
||||||
|
(dog-name my-pet) ; => "lassie"
|
||||||
|
|
||||||
|
; Pairs
|
||||||
|
; "cons" constructs pairs, "car" and "cdr" extract the first
|
||||||
|
; and second elements
|
||||||
|
(cons 1 2) ; => '(1 . 2)
|
||||||
|
(car (cons 1 2)) ; => 1
|
||||||
|
(cdr (cons 1 2)) ; => 2
|
||||||
|
|
||||||
; Lists are linked-list data structures
|
; Lists are linked-list data structures
|
||||||
'(1 2 3)
|
'(1 2 3) ; => '(1 2 3)
|
||||||
|
|
||||||
; Vectors are fixed-length arrays
|
; Vectors are fixed-length arrays
|
||||||
#(1 2 3)
|
#(1 2 3) ; => '#(1 2 3)
|
||||||
|
|
||||||
; Use "cons" to add an item to the beginning of a list
|
; Use "cons" to add an item to the beginning of a list
|
||||||
(cons 4 '(1 2 3)) ; => (4 1 2 3)
|
(cons 4 '(1 2 3)) ; => (4 1 2 3)
|
||||||
@ -167,8 +178,8 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
;; 3. Functions
|
;; 3. Functions
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Use lambda to create new functions. A function always returns
|
; Use lambda to create new functions.
|
||||||
; its last statement.
|
; A function always returns its last statement.
|
||||||
(lambda () "Hello World") ; => #<procedure>
|
(lambda () "Hello World") ; => #<procedure>
|
||||||
|
|
||||||
; (You need extra parens to call it)
|
; (You need extra parens to call it)
|
||||||
@ -210,7 +221,8 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
;; 4. Control Flow
|
;; 4. Control Flow
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Conditionals
|
;;; Conditionals
|
||||||
|
|
||||||
(if #t ; test expression
|
(if #t ; test expression
|
||||||
"this is true" ; then expression
|
"this is true" ; then expression
|
||||||
"this is false" ; else expression
|
"this is false" ; else expression
|
||||||
@ -222,20 +234,61 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
'yep
|
'yep
|
||||||
'nope) ; => 'yep
|
'nope) ; => 'yep
|
||||||
|
|
||||||
|
|
||||||
; "cond" chains a series of tests to select a result
|
; "cond" chains a series of tests to select a result
|
||||||
(cond
|
(cond
|
||||||
[(> 2 2) (error "wrong!")]
|
[(> 2 2) (error "wrong!")]
|
||||||
[(< 2 2) (error "wrong again!")]
|
[(< 2 2) (error "wrong again!")]
|
||||||
[else 'ok]) ; => 'ok
|
[else 'ok]) ; => 'ok
|
||||||
|
|
||||||
; Pattern matching
|
;;; Pattern Matching
|
||||||
|
|
||||||
; Loops
|
(define (fizzbuzz? n)
|
||||||
|
(match (list (remainder n 3) (remainder n 5))
|
||||||
|
[(list 0 0) 'fizzbuzz]
|
||||||
|
[(list 0 _) 'fizz]
|
||||||
|
[(list _ 0) 'buzz]
|
||||||
|
[_ #f]))
|
||||||
|
|
||||||
; Sequences
|
(fizzbuzz? 15) ; => 'fizzbuzz
|
||||||
|
(fizzbuzz? 37) ; => #f
|
||||||
|
|
||||||
|
;;; Loops
|
||||||
|
|
||||||
|
(define (loop i)
|
||||||
|
(when (< i 10)
|
||||||
|
(printf "i:~a~n" i)
|
||||||
|
(loop (add1 i))))
|
||||||
|
|
||||||
|
(loop 5) ; => i:5 i:6 ...
|
||||||
|
|
||||||
|
(let loop ((i 0))
|
||||||
|
(when (< i 10)
|
||||||
|
(printf "i:~a~n" i)
|
||||||
|
(loop (add1 i)))) ; => i:0 i:1 ...
|
||||||
|
|
||||||
|
|
||||||
|
;;; Sequences
|
||||||
|
|
||||||
|
; "for" allows iteration over sequences:
|
||||||
|
; lists, vectors, strings, sets, hash tables, etc...
|
||||||
|
(for ([i (in-list '(l i s t))])
|
||||||
|
(displayln i))
|
||||||
|
|
||||||
|
(for ([i (in-vector #(v e c t o r))])
|
||||||
|
(displayln i))
|
||||||
|
|
||||||
|
(for ([i (in-string "string")])
|
||||||
|
(displayln i))
|
||||||
|
|
||||||
|
(for ([i (in-set (set 'x 'y 'z))])
|
||||||
|
(displayln i))
|
||||||
|
|
||||||
|
(for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))])
|
||||||
|
(printf "key:~a value:~a ~n" k v))
|
||||||
|
|
||||||
|
|
||||||
|
;;; Exceptions
|
||||||
|
|
||||||
; Exceptions
|
|
||||||
; To catch an exception, use the "with-handlers" form
|
; To catch an exception, use the "with-handlers" form
|
||||||
; To throw an exception use "raise"
|
; To throw an exception use "raise"
|
||||||
(with-handlers
|
(with-handlers
|
||||||
@ -246,11 +299,12 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 5. Modules
|
;; 5. Modules
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
; Modules let you organize code into multiple files and reusable libraries.
|
|
||||||
|
|
||||||
(module cake racket/base ;; define a new module 'cake' based on racket/base
|
; Modules let you organize code into multiple files and reusable libraries
|
||||||
|
|
||||||
(provide print-cake) ;; function exported by the module
|
(module cake racket/base ; define a new module 'cake' based on racket/base
|
||||||
|
|
||||||
|
(provide print-cake) ; function exported by the module
|
||||||
|
|
||||||
(define (print-cake n)
|
(define (print-cake n)
|
||||||
(show " ~a " n #\.)
|
(show " ~a " n #\.)
|
||||||
@ -262,7 +316,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
(printf fmt (make-string n ch))
|
(printf fmt (make-string n ch))
|
||||||
(newline)))
|
(newline)))
|
||||||
|
|
||||||
;; Use "require" to import all functions from the module
|
; Use "require" to import all functions from the module
|
||||||
(require 'cake)
|
(require 'cake)
|
||||||
(print-cake 3)
|
(print-cake 3)
|
||||||
;(show "~a" 1 #\A) ; => error, "show" was not exported
|
;(show "~a" 1 #\A) ; => error, "show" was not exported
|
||||||
@ -302,6 +356,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 7. Macros
|
;; 7. Macros
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Macros let you extend the syntax of the language
|
; Macros let you extend the syntax of the language
|
||||||
(define-syntax-rule (unless test then else)
|
(define-syntax-rule (unless test then else)
|
||||||
(if test else then))
|
(if test else then))
|
||||||
|
Loading…
Reference in New Issue
Block a user