;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname func-call-and-sigs-before) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; A hello-world program: "Hello, world." ; Racket is an industrial-strength scheme ; Scheme is an elegant, minimal version of Lisp ; Lisp is the 2nd-oldest high-level language still in use (after Fortran) ; (Lisp ~ 1965) ; When calling `substring`, we pass it 3 inputs (string, natural-number, natural-number), ; and we get a string back. ; In Ada, python: substring("Mississippi", 2, 5) (substring "Mississippi" 2 5) ; When calling '+', we pass it 2 [or more] inputs (number, number) ; and we get a number back ;; In Ada, python: 2 + 3 ;; "operators" are just functions that are called with a different syntax. (+ 2 3) ; IN racket: `(` means 'call a function'. ; In java, `(` was part of 'call a function', like `Math.sqrt(16)` ; but it is also part of expressions, like `(1+Math.sqrt(5))/2` ; ; Q1: What is the *syntax* for calling a function in racket? ; ; Q2: Well first, to compare: what is the *syntax* for calling a static-method in Java: ; A2: ;;;TODO ; ; Note which parts are literally-required (punctuation (& keywords, if any)), ; and which parts are to be replaced by something of-that-sort. ; ; Now, back to our first question: ; Q1: Can you guess the syntax for calling a function in racket? ; A1: ;;;TODO ; ; ; Give the racket version of the math-expression: (1+√5)/2 ;;; TODO in class ; Relevent: compare this to f( g(1,h(5)), 2) ; ; Compare: somePerson.getName().substring(0,5).toUpperCase(); ; to f( g(h(somePerson),0,5) ) ; Note how we are simply repeating (and composing) the syntax for function-call ; ...a function-call is one type of ``, whatever an `` is! #| Racket: Examples of provided data types: number (real, rational, integer) 2.3 -47 #i3.14 boolean #false #true char #\A #\right string "Mississippi" symbol 'MS 'VA 'TX 'NY 'bart-simpson (...and many others: regexp #px"[0-9]*", images, ports, etc) |# #| Some built-in functions and their signatures (types): Signature Example substring : string, natnum, natnum -> string (substring "hello" 1 4) = "ell" string-ref : ______, ______ -> ______ (string-ref "hello" 1) = #\e string-length : ______ -> ______ (string-length "hello") = 5 string-append : ______, ______ -> ______ (string-append "hel" "lo") = "hello" = : number, number -> boolean (= 4 (+ 2 3)) = #false string=? : ______, ______ -> _______ (string=? "ell" (substring "hello" 1 4)) = #true string? : ______ -> ______ (string? 43) = #false number->string : ______ -> ______ (number->string (* 7 6)) = "42" char-downcase : ______ -> ______ (char-downcase #\A) = #\a TODO in class: fill in the above blanks; note naming conventions. |# ; We saw how to express (1+√5)/2 earlier ... BUT what if I want ; to use that value many many times -- do I have to re-write it? ; That need motivates our first racket keyword: #| `define`: a keyword to bind an identifier to a value: |# (define n 37) (define m (* n 99)) (define ϕ (/ (+ 1 (sqrt 5)) 2)) (define fname "Ian") (define lname "B-meister") ; Syntax for define-an-identifier: ; ;;;TODO in class ; While I might call these "variables", ; in functional-programming we won't ever RE-assign to an identifier, ; so they won't actually vary as the program runs. ; Hence, "identifier". ; (But if I do slip up and say "variable" in the context of FP, ; you'll know that I mean "identifier".)