;; 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) (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) ; We will NOT be learning you racket, in this class. ; Well, we'll learn about 5 basic pieces of syntax (fewer than are in HelloWorld.java), ; and then see how much we can do with those. ; (Why? Partly to be exposed to a minimal language, to understand/appreciate trade-offs ; involved with having many keywords/features like Java, C++, or full-racket.) ; More fundamentally, though: we WILL introduce you to using immutable data ; (which you can do in ANY language … ; though many others make it harder than racket, since libraries often don't ; co-operate with an immutable approach). #| Racket: Examples of built-in data types: number (real, rational, integer) 2.3 -47 #i3.14 [we leave other numeric-literals for later (vocab)] boolean #false #f false #true #t true [distinguish a literal, from a variable/identifier] char #\A #\b #\right string "Mississippi" symbol 'MS 'VA 'TX 'NY 'bart-simpson (...and others: regexp #px"[0-9]*", images, ports, etc) Vocab "value" -- a piece of information used a program; can be passed/returned from functions; the result of evaluating an expression. Btw, the above examples are all *literal* values [known at compile-time]. Vocab "literal": A value which appears in the source-code [e.g. all the "examples" shown above.] There are rules -- "syntax" -- for exactly what sequences-of-characters are allowed for teach type of . Another provided data-type: functions (!?). Examples: substring sqrt * (function(n) (+ n 17)) |# ;;; How to call a function ; 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', period. ; In java, `(` was part of 'call a function', like `Math.sqrt(16)` ; but it can also mean "precedence operator", like `(1+Math.sqrt(5))/2`. ; (And fwiw, it can also mean "casting".) ; ; ; Vocab term "expression": ; a chunk of program that evaluates to a value: ; (substring "hello" 2 3), as well as immediate values `3` and `"hello"`. ; But *not* a define-statement -- that doesn't give an answer, it just remembers a def'n ("binds an identifier"). ; ; ; ; 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: (, ...) ((Note to prof: on board use different colors, not "<>")) ; Note which parts are literally-required (punctuation (& keywords, if any)), ; and which parts are to be replaced by something of-that-sort. ; Btw, later we'll learn how to specify syntax more carefully, ; to show that 0 args allowed and get commas right. ; ; Now, back to our first question: ; Q1: Can you guess the syntax for calling a function in racket? ; A1: is: ( …) ; Give the racket version of the math-expression: (1+√5)/2 ;;; DONE in class ; (/ (+ 1 (sqrt 5)) 2) ; You can approach this "top down" OR "bottom up", whichever works for you. ; - Top down: ; (/ ___ ___) (we'll proceed fill in each blank with a sub-) ; ⇒ (/ ___ 2) ; ⇒ (/ (+ __ __) 2) ; ⇒ (/ (+ 1 __) 2) ; ⇒ (/ (+ 1 (sqrt __)) 2) ; ⇒ (/ (+ 1 (sqrt 5) 2) ; ; - Bottom up: ; 5 ; … (sqrt 5) ; … 1 ; … (+ 1 (sqrt 5)) ; … 2 ; … (/ (+ 1 (sqrt 5)) 2) ; 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! #| An is: - a , OR - an , OR - a [anything further cases / sorts of s? We'll see a 2-3 other "special forms" later: `cond`, `let` (as well as `and`,`or` which *look* like functions but have short-circuiting behavior.)] |# #| Some built-in functions and their signatures (types): Signature Example string-ref : string natnum -> char (string-ref "hello" 1) = #\e string-length : string -> natnum (string-length "hello") = 5 substring : string natnum natnum -> string (substring "hello" 1 4) = "ell" |# #| Some built-in functions and their signatures (types): Signature Example substring : (-> string? natnum? natnum? string?) (substring "hello" 1 4) = "ell" string-ref : (-> string? natnum? char?) (string-ref "hello" 1) = #\e string-length : (-> string? natnum?) (string-length "hello") = 5 string-append : (-> string? string? string?) (string-append "hel" "lo") = "hello" char-downcase : (char-downcase #\B) = #\b = : (-> number? number? boolean?) (= 4 (+ 2 3)) = #false string=? : (-> string? string? boolean?) (string=? "ell" (substring "hello" 1 4)) = #true string? : (-> any? boolean?) (string? 43) = #false number->string : (-> number? string?) (number->string (* 7 6)) = "42" char-downcase : (-> char? char?) (char-downcase #\A) = #\a ; DONE in class: fill in the above; note naming-conventions. |# ; We saw how to express (1+√5)/2 earlier ... BUT what if we want ; to use that value many many times -- do we have to re-write it ; (and have it re-computed each time)? ; We *could* (I mean, it wouldn't be an incorrect program), ; but of course we'd like to have the computer *remember* the value ; for future use. ; 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: ; (define ) ; 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".) ;;;; example of defining a function in racket: ; return the 2nd char in a string. (define (second-char wrd) ; (-> string-length>=2? char?) ; OR: use `string?` and have a written pre-condition (string-ref wrd 1)) (check-expect (second-char "hello") #\e) (check-expect (second-char "by") #\y) (check-expect (second-char "hello") #\e) (check-expect (second-char "dinosaur") #\i) (check-expect (second-char "zz") #\z) (check-expect (second-char " ") #\space)