#lang scribble/doc @(require scribble/manual scribble/eval scribble/basic scribble/bnf (planet cce/scheme:4:1/planet) (for-label (planet dherman/pprint:4) scheme/base scheme/contract "../main.ss") "util.ss") @title[#:tag "top"]{@bold{Infix Expressions} for PLT Scheme} @author[(author+email "Jens Axel Søgaard" "jensaxel@soegaard.net")] This package provides infix notation for writing mathematical expressions. @section{Getting Started} A simple example, calculating 1+2*3. @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) (display (format "1+2*3 is ~a\n" @${1+2*3} ) }| @subsection{Arithmetical Operations} The arithmetical operations +, -, *, / and ^ is written with standard mathematical notation. Normal parentheseses are used for grouping. @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) @${2*(1+3^4)} ; evaluates to 164 }| @subsection{Identifiers} Identifiers refer to the current lexical scope: @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) (define x 41) @${ x+1 } ; evaluates to 42 }| @subsection{Application} Function application use square brackets (as does Mathematica). Here @scheme[sqrt] is bound to the square root function defined in the language after at-exp, here the scheme language. @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) (display (format "The square root of 64 is ~a\n" @${sqrt[64]} )) @${ list[1,2,3] } evaluates to the list (1 2 3) }| @subsection{Lists} Lists are written with curly brackets {}. @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) @${ {1,2,1+2} } ; evaluates to (1 2 3) }| @subsection{List Reference} List reference is written with double square brackets. @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) (define xs '(a b c)) @${ xs[[1]] } ; evaluates to b }| Note: Since ]] denotes "closing double square brackets", one currently needs to insert a space in nested function applications: @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) (define xs '(a b c)) @${ exp[log[10] ] } }| @subsection{Anonymous Functions} The syntax (λ ids . expr) where ids are a space separated list of identifiers evaluates to function in which the ids are bound in body expressions. @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) @${ (λ.1)[]} ; evaluates to 1 @${ (λx.x+1)[2]} ; evaluates to 3 @${ (λx y.x+y+1)[1,2]} ; evaluates to 4 }| @subsection{Square Roots} Square roots can be written with a literal square root: @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) @${√4} ; evaluates to 2 @${√(2+2)} ; evaluates to 2 }| @subsection{Comparisons} The comparison operators <, =, >, <=, and >= are available. The syntaxes ≤ and ≥ for <= and >= respectively, works too. Inequality is tested with <>. @subsection{Logical Negation} Logical negations is written as ¬. @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) @${ ¬true } ; evaluates to #f @${ ¬(1<2) } ; evaluates to #f }| @subsection{Assignment} Assignment is written with := . @subsection{Sequencing} A series of expresions can be evaluated by interspersing semi colons between the expressions. @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) (define x 0) @${ (x:=1); (x+3) } ; evaluates to 4 }| @section{Examples} @subsection{Example: Fibonacci} This problem is from the Euler Project. Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... Find the sum of all the even-valued terms in the sequence which do not exceed four million. @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix) (only-in (planet "while.scm" ("soegaard" "control.plt" 2 0)) while)) (define-values (f g t) (values 1 2 0)) (define sum f) @${ while[ g< 4000000, when[ even?[g], sum:=sum+g]; t := f + g; f := g; g := t]; sum}| @subsection{Example: Difference Between a Sum of Squares and the Square of a Sum} This problem is from the Euler Project. The sum of the squares of the first ten natural numbers is, 1^2 + 2^2 + ... + 10^2 = 385 The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)^2 = 552 = 3025 Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640. Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) (require (planet "while.scm" ("soegaard" "control.plt" 2 0))) ; while #| (define n 0) (define ns 0) (define squares 0) (define sum 0) @${ sum:=0; while[ n<100, n := n+1; ns := ns+n; squares := squares + n^2]; ns^2-squares } }| @subsection{Example: Pythagorean Triplets} This example is from the Euler Project. A Pythagorean triplet is a set of three natural numbers, a,b,c for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) (let-values ([(a b c) (values 0 0 0)]) (let/cc return (for ([k (in-range 1 100)]) (for ([m (in-range 2 1000)]) (for ([n (in-range 1 m)]) @${ a := k* 2*m*n; b := k* (m^2 - n^2); c := k* (m^2 + n^2); when[ a+b+c = 1000, display[{{k,m,n}, {a,b,c}}]; newline[]; return[a*b*c] ]}))))) }| @subsection{Example: Miller Rabin Primality Test} This example was inspired by Programming Praxis: http://programmingpraxis.com/2009/05/01/primality-checking/ @verbatim[#:indent 2]|{ #lang at-exp scheme (require (planet soegaard/infix)) (require srfi/27) ; random-integer (define (factor2 n) ; return r and s, s.t n = 2^r * s where s odd ; invariant: n = 2^r * s (let loop ([r 0] [s n]) (let-values ([(q r) (quotient/remainder s 2)]) (if (zero? r) (loop (+ r 1) q) (values r s))))) (define (miller-rabin n) ; Input: n odd (define (mod x) (modulo x n)) (define (expt x m) (cond [(zero? m) 1] [(even? m) @${mod[sqr[x^(m/2)] ]}] [(odd? m) @${mod[x*x^(m-1)]}])) (define (check? a) (let-values ([(r s) (factor2 (sub1 n))]) ; is a^s congruent to 1 or -1 modulo n ? (and @${member[a^s,{1,mod[-1]}]} #t))) (andmap check? (build-list 50 (λ (_) (+ 2 (random-integer (- n 3))))))) (define (prime? n) (cond [(< n 2) #f] [(= n 2) #t] [(even? n) #f] [else (miller-rabin n)])) (prime? @${2^89-1}) }|