diff --git a/Racket/hw4m.rkt b/Racket/hw4m.rkt new file mode 100644 index 0000000..3b6289e --- /dev/null +++ b/Racket/hw4m.rkt @@ -0,0 +1,99 @@ + +#lang racket + +(provide (all-defined-out)) ;; so we can put tests in a second file + +;; put your code below + +; basic function +(define cube1 + (lambda (x) + (* x (* x x)))) + +; many functions, such as *, take a variable number of arguments +(define cube2 + (lambda (x) + (* x x x))) + +; syntactic sugar for function definitions +(define (cube3 x) + (* x x x)) + + +; Problem 1 + +(define (sequence_bad_style low high stride) + (if (> (+ low stride) high) + ( if (> low high) null (list low) ) + (cons low (sequence (+ low stride) high stride)) + ) + ) + + +(define (sequence low high stride) + ( cond + [ (> (+ low stride) high) null] + [ (> low high) (list low)] + [ #t (cons low (sequence (+ low stride) high stride))] + ) + + ) + +; Problem 2 +(define (string-append-map xs suffix) + (map + (lambda (x) + (string-append x suffix) + ) + xs + ) + ) + +; Problem 3 +(define (list-nth-mod xs n) + (cond + [ (null? xs) (error "list-nth-mod: empty list")] + [ (< n 0) (error "list-nth-mod: negative number") ] + [ #t (last + (take + xs + (+ + 1 + (modulo + (length xs) + n + ) + ) + ) + )] + ) + ) + + +(define ones (lambda () (cons 1 ones))) + +(define (f x) (cons x ( lambda () (f ( + x 1)) ))) +(define nats1 ( lambda() (f 1))) + +(define (stream-maker fn arg) + (letrec ([f (lambda (x) + (cons x (lambda () (f (fn x arg)))))]) + (lambda () (f arg)))) + +(define nats2 (stream-maker + 1)) +(define powers2 (stream-maker * 2)) + + +(define (funny_addition x y ) + (letrec + ( + [sum (+ (abs x) (abs y) )] + ) + ( cond + [ (= (modulo sum 5 ) 0) (* sum -1)] + [ #t sum] + ) + ) + ) + +(define funny-number-stream (stream-maker funny_addition 1)) \ No newline at end of file diff --git a/hw3provided.txt b/hw3provided.txt new file mode 100644 index 0000000..947d010 --- /dev/null +++ b/hw3provided.txt @@ -0,0 +1,105 @@ +(* Coursera Programming Languages, Homework 3, Provided Code *) + +exception NoAnswer + +datatype pattern = Wildcard + | Variable of string + | UnitP + | ConstP of int + | TupleP of pattern list + | ConstructorP of string * pattern + + +datatype valu = Const of int + | Unit + | Tuple of valu list + | Constructor of string * valu + +fun g f1 f2 p = + let + val r = g f1 f2 + in + case p of + Wildcard => f1 () + | Variable x => f2 x + | TupleP ps => List.foldl (fn (p,i) => (r p) + i) 0 ps + | ConstructorP(_,p) => r p + | _ => 0 + end + +(**** for the challenge problem only ****) + +datatype typ = Anything + | UnitT + | IntT + | TupleT of typ list + | Datatype of string + +(**** you can put all your code here ****) + +(* 1 *) + +fun only_capitals strings = + List.filter (fn x => Char.isUpper(String.sub(x,0))) strings + +(* 2 *) + +fun longest_string1 strings = + List.foldl ( fn (x,y) => if String.size(x) > String.size(y) then x else y ) "" strings + +(* 3 *) + +fun longest_string2 strings = + List.foldl ( fn (x,y) => if String.size(x) >= String.size(y) then x else y ) "" strings + + +(* 4 *) +fun longest_string_helper(compare_function,strings) = + List.foldl ( fn (x,y) => if compare_function(String.size(x),String.size(y)) then x else y ) "" strings + + +fun longest_string3 strings = + let + val compare_function = fn (x,y) => x > y ; + in + longest_string_helper(compare_function,strings) + end + + +fun longest_string4 strings = + let + val compare_function = fn (x,y) => x >= y ; + in + longest_string_helper(compare_function,strings) + end + +(* 5 *) +fun longest_capitalized strings = + (longest_string2 o only_capitals) strings + +(* 6 *) +fun rev_string x = + (String.implode o List.rev o String.explode) x + +(* 7 *) +fun first_answer(f,xs) = + case List.mapPartial f xs of + [] => raise NoAnswer + | v::vs => v + +(* 8 *) +fun all_answers f xs = +let fun helper (x, acc) = + case x of + [] => acc + | head :: tail => case f head of + NONE => [] + | SOME v => helper(tail,(v) @ acc) +in + case (helper(xs,[])) of + [] => NONE + | head::tail => SOME (head::tail) +end + + +