Sunday, 21 July 2013

Basics of Racket

Racket its language based on lisp its have some primitive data type and data structures
   Some important built-in data types are Boolean,Number,Character,String
Bytes,Keywords, Symbols,Hash Tables,List and Pairs  etc

Boolean
  racket has two distinguishable values #t and #f which is known as boolean data types  

examples:
 > (= 2 (+ 1 1))  
 #t  
 > (boolean? #t)  
 #t  
 > (boolean? #f)  
 #t  
 > (boolean? "no")  
 #f  
 > (if "no" 1 0)  
 1  

Numbers
   number categorize by integer complex floating point etc

examples:
 > (integer? 5)  
 #t  
 > (complex? 5)  
 #t  
 > (integer? 5.0)  
 #t  
 > (integer? 1+2i)  
 #f  
 > (complex? 1+2i)  
 #t  
 > (complex? 1.0+2.0i)  
 #t  

Keywords
A keyword value is similar to a symbol  but its printed form is prefixed with #:

examples:
 > (string->keyword "apple")  
 '#:apple  
 > '#:apple  
 '#:apple  
 > (eq? '#:apple (string->keyword "apple"))  
 #t  

List and pairs
A pair joins two arbitrary values. The cons procedure constructs pairs, and the
car and cdr procedures extract the first and second elements of the pair, respectively
examples:
 > (cons 1 2)  
 '(1 . 2)  
 > (cons (cons 1 2) 3)  
 '((1 . 2) . 3)  
 > (car (cons 1 2))  
 1  
 > (cdr (cons 1 2))  
 2  
 > (pair? (cons 1 2))  
 #t  

A list is a combination of pairs that creates a linked list. More precisely, a list is either the empty list null, or it is a pair whose first element is a list element and whose second element is a list. The list? predicate recognizes lists. The null? predicate recognizes the empty list.

examples:
 > (list 1 2 3 4)  
 '(1 2 3 4)  
 > (list (list 1 2) (list 3 4))  
 '((1 2) (3 4))  

basic procedures that used for operations on a list are

 > (map (lambda (i) (/ 1 i))  
     '(1 2 3))  
 '(1 1/2 1/3)  
 > (andmap (lambda (i) (i . < . 3))  
      '(1 2 3))  
 #f  
 > (ormap (lambda (i) (i . < . 3))  
      '(1 2 3))  
 #t  
 > (filter (lambda (i) (i . < . 3))  
      '(1 2 3))  
 '(1 2)  
 > (foldl (lambda (v i) (+ v i))  
      10  
      '(1 2 3))  
 16  
 > (for-each (lambda (i) (display i))  
       '(1 2 3))  
 123  
 > (member "Keys"  
      '("Florida" "Keys" "U.S.A."))  
 '("Keys" "U.S.A.")  
 > (assoc 'where  
      '((when "3:30") (where "Florida") (who "Mickey")))  
 '(where "Florida")  

Hash Tables
A hash table implements a mapping from keys to values, where both keys and values can be arbitrary Racket values, and access and update to the table are normally constant-time operations. Keys are compared using equal?, eqv?, or eq?, depending on whether the hash table is created with make-hash, make-hasheqv, or make-hasheq.

examples:
 > (define ht (make-hash))  
 > (hash-set! ht "apple" '(red round))  
 > (hash-set! ht "banana" '(yellow long))  
 > (hash-ref ht "apple")  
 '(red round)  
 > (hash-ref ht "coconut")  
 hash-ref: no value found for key  
  key: "coconut"  
 > (hash-ref ht "coconut" "not there")  
 "not there"  

This are important data types in racket

to know more about racket go to http://docs.racket-lang.org/guide/

No comments:

Post a Comment