User talk:122.177.213.141/Sandbox

DEFINING VARIABLES (define  ) define is used to define variables required in the program. It can only be used immediately after the function header and before any expression is written in the program. Variables are never put in parenthesis. Initial value could be directly a value, or an expression that would result into a value to be assigned to variable (define myNumber 5) ; myNumber will be declared with initial value of 5 (define myVar (+ 4 5)) ; myVar will be declared with initial value of 9 (define myString "hello") ; myString will be declared with initial value of "hello" (define myList (list 1 2 3)) ; best way of declaring lists, myList will be declared with initial value of (1 2 3) (define myList (list)) ; empty list - 1st method, myList will be declared with initial value of (define myList ') ; empty list - 2nd method, myList will be declared with initial value of (define myList null) ; empty list - 3rd method, myList will be declared with initial value of (define mySymbol 'Hello) ; mySymbol will be declared with initial value of Hello UPDATING VARIABLE VALUES (set!  ) set! can only be used on variables that have already been defined earlier using the define keyword New value could be directly a value, or an expression that would result into a value to be assigned to variable (set! myVar (+ 25 20)) ; myVar will be updated with new value of 45 (set! myString "Hello World") ; myString will be updated with new value of "Hello World" DEFINING PROCEDURES When defining procedures, procedure name along with the arguments is put in parenthesis (define (  . . . ) .. . ) (define (findsum num1 num2) (+ num1 num2) ) (findsum 100 200) ; will return 300 COMMON STRING FUNCTIONS (define myString "Hello World") (string-length ) ; For finding length of a string (string-length myString) ; returns the length of string - 11 in this case (string-ref  ) ; For getting a particular character in a string (string-ref myString 0) ; returns the character of string at given index, here index is 0, hence it returns the first character, i.e. #\H (string-append   . . . . ) ; For concatenating all the string variables given (string-append "Good" " " "Night") ; Forms a single string using Good, space and Night, i.e. “Good Night” (string-append "Say" " " myString) ; Forms a single string using Say, space and myString variable value, i.e. "Say Hello World" (string=?  ) ; For comparing two strings, returns #t if string match exactly (case-wise also) (string=? "pen" "Pen") (string=? "pen" "pen") (string-ci=? <string_var1> <string_var2>) ; For comparing two strings, returns #t if string match exactly (case insensitive) (string-ci=? "pen" "Pen") COMMONLY USED PREDICATES (string? <myVar>) ; returns #t if myVar is a string, else returns #f (symbol? <myVar>) ; returns #t if myVar is a symbol, else returns #f (number? <myVar>) ; returns #t if myVar is a number, else returns #f (list? <myVar>) ; returns #t if myVar is a list, else returns #f (<structure_name>? <myVar>) ; returns #t if myVar is a structure instance of the given type, else returns #f (integer? <myVar>) ; returns #t if myVar is an integer, else returns #f (positive? <myVar>) ; returns #t if myVar is greater than zero, else returns #f COMMON LIST FUNCTIONS (length <list_variable_name>) ; Used to determine the number of elements in the list (length (list 1 2 3 4 5 6 7)) ; returns the length of list, in this case - 7 (length (list "This" "is" "an" "example")) ; returns the length of list, in this case - 4 (list-ref <list_variable_name> <index_position>) ; For getting a particular element of a list (define myList (list 4 5 6)) (list-ref myList 0) ; returns the 1st element of list, i.e. 4 (list-ref myList 1) ; returns the 1st element of list, i.e. 5 (car <list_variable>) ; For getting the first element of the list (car myList) ; will return the first element of list, i.e. 4, return type is a normal value (i.e. non-list) (cdr <list_variable>) ; For getting the entire list except the first element (cdr myList) ; will return a list except the first element of list, i.e. (5 6), return type is always list (append <list_variable_1> <list_variable_2>) ; For appending two list variable elements into one list (define list1 (list 1 2 3)) (define list2 (list 4 5 6)) (append list1 list2) ; will append both lists into a single list, i.e. (1 2 3 4 5 6) (append list1 7) ; will result in (1 2 3 . 7) (append list1 (list 7)) ; will result in (1 2 3 7) original list, to this new list. This new list can finally be assigned to original list COMMON STRUCTURE FUNCTIONS (define-struct <structure_name> (<attribute_1> <attribute_2>. . . . <attribute_n>)#f) (define-struct Student (rollno firstname lastname)#f) ; defines a structure named Student having rollno, firstname and lastname as attributes (make-<structure_name> <attribute_1> <attribute_2> . . . . <attribute_n>) (make-Student 1 "Amit" "Shah") ; creates a structure instance (define myStr (make-Student 1 "Amit" "Shah")) ; assigning the structure instance to myStr variable (<structure_name>-<attribute_name> <structure_instance>) ; For getting the value of particular attribute of a structure instance (Student-firstname myStr) ; will return the first name of myStr, i.e. "Amit" (set-<structure_name>-<attribute_name> <structure_instance> <new_value>) ; For updating the value of particular attribute of a structure instance (set-Student-firstname! myStr “Pankaj”) ; will update the first name of myStr, to "Pankaj" (Student-firstname myStr) ; will return the updated name of myStr, i.e. "Pankaj" IF statement ;It is generally used when only one condition is to be evaluated (if <Conditional_expression> <true_part> <false_part>) (if (< 5 10) "First number is small" "Second number is small") statements in either true or false part, then we need to use the begin block (if (< 5 10) ; starting parenthesis for if (begin ; start of true part (display "First number is smaller" ) (newline) (display "S") ) (begin ; start of false part "First number is larger" (newline) (display "L") ) ) ; closing parenthesis for if COND statement ;It is generally used when multiple conditions are to be evaluated (cond [(<Conditional_Expr_1>) <true_part>] [(<Conditional_Expr_2>) <true_part>] .. [(<Conditional_Expr_n>) <true_part>] [else <default_part>] ; else keyword is optional ) (define age 68) (cond [(<= age 19) "Teenager"] [(<= age 60) "Adult"] [else "Senior"] ) (define age 40) (define gender "F") (if (and (string=? gender "F") (> age 35)) "Eligible for discount" "Not eligible") (define age 30) (define gender "F") (if (and (string=? gender "F") (> age 35)) "Eligible for discount" "Not eligible") (define age 30) (define gender "F") (if (or (string=? gender "F") (> age 35)) "Eligible for discount" "Not eligible") (define age 40) (define gender "F") (if (not (and (string=? gender "F") (> age 35))) "Not eligible" "Eligible for discount") ITERATION USING TAIL RECURSION: let keyword allows to define local procedures and local variables. Scope of these procedures and variables is restricted to the opening and closing parenthesis of let block. (let <local_procedure_name> ((local_var1 initial_value1)(local_var2 initial_value2). . . . (local_varN initial_valueN)) .. . ) initialized to 1. numbers from 1 to 10, with each number printed on new line. (let numberloop ((cnt 1)) (if (<= cnt 10) ;base condition (loop terminiation condition) (begin (newline)(display cnt) (numberloop (+ cnt 1)) ; recursive call ) ) ) SAMPLE PROGRAM # 1 Write a function FindSum that accepts two numbers as input and returns the sum of all numbers between those limits (inclusive of limits). Following validations need to be taken care of in given order: 1) If any of the inputs is not a number, return -1 2) If any of the inputs is zero or negative, return -2 SOLUTION: the sum of all numbers between those limits (inclusive of limits). If either of the input parameters is not a number, it returns -1. If either of the input parameters is zero or negative, returns -2 (define (FindSum lowerlimit upperlimit) (define flag 0) (define sum 0) (set! flag (validation lowerlimit upperlimit)) (if (= flag 0) ; compute sum only for valid values (begin (let numloop ((num lowerlimit)) (if (<= num upperlimit) (begin (set! sum (+ sum num)) (numloop (+ num 1)) ) ) ) sum ; return sum for valid values ) flag ; return flag for invalid values ) ) not a number, it returns -1. If either of the input parameters is zero or negative, returns -2. If validations are through, returns 0. (define (validation lowerlimit upperlimit) (cond [(not (number? Lowerlimit)) -1] ; Data validation [(not (number? Upperlimit)) -1] ; Data validation [(not (positive? lowerlimit)) -2] ; Business validation [(not (positive? upperlimit)) -2] ; Business validation [(< upperlimit lowerlimit) -2] ; Business validation [else 0] ; returns 0, when no errors ) ).
 * Number
 * String
 * List
 * Symbol
 * Body of procedure
 * defining a procedure
 * findsum is a procedure that takes num1 and num2 are inputs and returns their sum
 * calling the above defined procedure
 * Appending a non-list variable to a list will result in a dotted pair
 * To add an element to existing list, convert it to a list first
 * Removing element from a list
 * This can be achieved by creating an empty list first and then appending only the required values from
 * Defining a Structure
 * Creating Structure instance
 * Assigning structure instance to a variable
 * Accessing structure attributes
 * Updating structure attributes
 * In above case, the true and false part contain only 1 statement. If we require to execute multiple
 * Conditional Expressions using "and", "or" and "not"
 * AND - All given conditions need to satisfied
 * OR - Any one of the given conditions need to satisfied
 * NOT - Negating the condition
 * Body of local procedure
 * The below code block uses Let to define a local procedure "numberloop" with a local variable "cnt"
 * This local procedure is recursively called till "cnt" is less than or equal to 10. The function displays
 * Contract: FindSum : number number -> number
 * Purpose: This function accepts two numbers, a lower limit and an upper limit as input, and returns
 * Example: (one for each valid scenario)
 * (FindSum 1 4) → 10
 * Test Cases:(one for each valid and all invalid scenarios)
 * (FindSum 1 4) → 10
 * (FindSum “lower” 4) → -1
 * (FindSum 1 “upper”) → -1
 * (FindSum 0 4) → -2
 * (FindSum 1 -4) → -2
 * variable declarations
 * validations
 * logic
 * Helper / Auxillary function
 * Contract: validation: number number -> number
 * Purpose: This function validates the two parameters passed to it. If either of the input parameters is
 * Example: (one for each valid scenario)
 * (validation 1 4) → 0
 * Test Cases:(one for each valid and all invalid scenarios)
 * ( validation 1 4) → 0
 * ( validation “lower” 4) → -1
 * ( validation 1 “upper”) → -1
 * ( validation 0 4) → -2
 * ( validation 1 -4) → -2