Minute paper: Please design a function that determines if its parameter 
(a sequence of numbers) is sorted (in ascending order) or not. Remember
the design recipe from C211? 

; A ManyNumber is one of:
;   -- empty 

;   -- (cons Number ManyNumber)

(define e0 empty)
(define e1 (cons 3 e0))
(define e2 (cons 5 e1))
(define e3 (cons 1 e2))
(define e4 (list 1 4 3 2 5))

; sorted? : ManyNumber -> Boolean
; function determines if the sequence sorted in ascending
; given e0 expected: true 
; given e1 expected: true 
; given e2 expected: false
; given (list 2 3 5 8 9) expected true

; (define (sorted? lon)
;   (cond ((empty? lon) ...)
;         (else (... (first lon) ... (sorted? (rest lon)) ...))))
(define (sorted? lon)
  (cond ((empty? lon) true)
        ((empty? (rest lon)) true)
        ((< (first lon) (first (rest lon))) (sorted? (rest lon)))
        (else false)))

(check-expect (sorted? e0) true)
(check-expect (sorted? e1) true)
(check-expect (sorted? e2) false)
(check-expect (sorted? (list 1 2 3 5 4 6 7)) false)
(check-expect (sorted? (list 3 4 6 9)) true)

I am done. What other method can I use here to make it look more like a loop?

Can you implement this in accumulator based style? 

Can you implement this in Java? 

public class Wednesday {
  public static void main(String[] args) {
    System.out.println( Arrays.toString( args )); 
    System.out.println( Arrays.toString( Wednesday.convert ( args ) ) ); 
    System.out.println( Wednesday.sorted( Wednesday.convert( args ) ) ); 
  }
  public static boolean sorted(int[] a) {

    return false;  
  }
  public static int[] convert(String[] a) {
    int[] result;
    result = new int[ a.length ]; 
    
    return result; 
  }
}

Please develop in stages. 

Code above runs liek this:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Wednesday
[]
[]
false
> run Wednesday 5 2 4 3 6 1
[5, 2, 4, 3, 6, 1]
[0, 0, 0, 0, 0, 0]
false


import java.util.Arrays;

public class Wednesday {
  public static void main(String[] args) {
    System.out.println( Arrays.toString( args )); 
    System.out.println( Arrays.toString( Wednesday.convert ( args ) ) ); 
    System.out.println( Wednesday.sorted( Wednesday.convert( args ) ) ); 
  }
  public static boolean sorted(int[] a) {
    if (a.length <= 1) return true; 
    for (int i = 0; i < a.length-1; i = i + 1) {
      if (a[i] > a[i+1]) 
        return false; 
    }
    return true;  
  }
  public static int[] convert(String[] a) {
    int[] result;
    result = new int[ a.length ]; 
    for (int i = 0; i < a.length ; i = i + 1) {
      result[i] = Integer.parseInt( a[i] );  
    }
    return result; 
  }
}

This runs as follows:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Wednesday
[]
[]
true
> run Wednesday 3
[3]
[3]
true
> run Wednesday 3 2
[3, 2]
[3, 2]
false
> run Wednesday 3 4
[3, 4]
[3, 4]
true
> run Wednesday 1 2 3 5 4 6
[1, 2, 3, 5, 4, 6]
[1, 2, 3, 5, 4, 6]
false
> run Wednesday 1 2 3 4 5 6
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
true


Now. Can we write java.util.Arrays.sort(int[] a)? 

import java.util.Arrays;

public class Wednesday {
  public static void main(String[] args) {
    int[] a = { 5, 2, 3, 6, 1, 4 }; 
    System.out.println( Arrays.toString( a ) ); 
    Wednesday.sort( a ); 
    System.out.println( Arrays.toString( a ) ); 
    Arrays.sort( a ); 
    System.out.println( Arrays.toString( a ) );     
  }
  public static void sort(int[] a) {
    
  }
  public static boolean sorted(int[] a) {
    if (a.length <= 1) return true; 
    for (int i = 0; i < a.length-1; i = i + 1) {
      if (a[i] > a[i+1]) 
        return false; 
    }
    return true;  
  }
  public static int[] convert(String[] a) {
    int[] result;
    result = new int[ a.length ]; 
    for (int i = 0; i < a.length ; i = i + 1) {
      result[i] = Integer.parseInt( a[i] );  
    }
    return result; 
  }
}

      accumulator                     input 

         [ ]                     [5, 2, 3, 6, 1, 4]

Check the input if empty then done.

         [5]                     [   2, 3, 6, 1, 4]

Check the input if empty then done.

         [2, 5]                  [      3, 6, 1, 4]

 i n - e - t 

 a v
   _
  ( )

public static void insert(int[] accumulator, int number) {

  // really? 
}

It seems like the strategy here is clear.

(define (sort lon)
  (cond ((empty? lon) lon)
        (else (insert (first lon) (sort (rest lon))))))

(define (insert value lon)
; insert value in lon sorted ascending order already 
  ...)

import java.util.Arrays;

public class Wednesday {
  public static void main(String[] args) {
    int[] a = { 5, 2, 3, 6, 1, 4 }; 
    System.out.println( Arrays.toString( a ) ); 
    Wednesday.addTo(a, -1); 
    System.out.println( Arrays.toString( a ) );     
  }
  public static void addTo(int[] a, int value) {
    int[] b = Arrays.copyOf(a, a.length + 1); 
    b[b.length - 1] = value; 
    a = b; 
  }
}

Run and notice that -1 doesn't make it. 

Do we change the signature of insert? 

Do we keep void and approach things differently? 

Is there a fundamental limitation we're trying to overcome in addTo(..., ...)?

See you in lab. 

--