Skip to content
Snippets Groups Projects
Commit 669820d7 authored by James R. Wilcox's avatar James R. Wilcox
Browse files

sec02 worksheet

parent 47b46f7e
No related branches found
No related tags found
No related merge requests found
(* Section 02 worksheet *)
(** Recursion and non-recursion practice *)
(* Write a function increment_all of type int list -> int list that adds one to
all the elements of the input list. *)
(* YOUR BINDING HERE *)
(* Using these functions from lecture, write another implementation of
sum_of_first_n_ints that is not itself recursive, but only calls other
functions. *)
let rec countdown((n: int)) =
if n = 0
then []
else n :: countdown(n-1)
let rec sum((l: int list)) =
if l = []
then 0
else List.hd(l) + sum(List.tl(l))
(* YOUR BINDING HERE *)
(** Shadowing practice *)
let a = 1
let b = 2 + a
let a = "hi"
let c = b + 1
(* What does this code do? Does it typecheck? What values does each variable have? *)
(** Practice with triples *)
let fst3 (x,_,_) = x (* gets the first element of a triple *)
let snd3 (_,x,_) = x (* gets the second element of a triple *)
let thd3 (_,_,x) = x (* gets the third element of a triple *)
(* Write a function that takes two int triples (i.e., two arguments of type "int
* int * int") and returns whether the third component of the first argument
is equal to the first component of the second argument. *)
(* YOUR BINDING HERE *)
(* Write a function firsts3 that takes an (int * int * int) list and returns an
int list consisting of just the first components of all the triples in the
input list.
Hint: Similar to firsts from lec03.ml
*)
(* YOUR BINDING HERE *)
(** Debugging practice *)
(* The code below has several errors in it so we can try to debug them.
Uncomment it, then fix the errors. *)
(*
let x = 34
y = x + 1
let z = if y then 34 else x < 4
let q = if y > 0 then 0
let w = 0
let let = 34
let v = 4.0 / 2.0
let u = x / w
let 0cse341 = true
*)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment