Some Final Practice Problems¶
Practice Racket Programming Questions¶
In the following questions, pred is a predicate, i.e. a function that
takes one input and returns either #t or #f. If (pred x) is
#t, then we say that x satsifies pred.
Implement a function called
(some? pred lst)that returns#tif 1, or more, elements oflstsatisfypred, and#fotherwise. This is essentially the same as Racket’sormapfunction, so don’t useormap(orandmap) anywhere in your answers.Implement this two different ways:
Implement a function called
(all? pred lst)that returns#tif all the elements oflstsatisfypred, and#fotherwise. This is essentially the same as Racket’sandmapfunction, so don’t useandmap(orormap) anywhere in your answers. Implement this three different ways:Implement a function called
(none? pred lst)that returns#tif 0 elements oflstsatisfypred, and#fotherwise. Implement this three different ways:Implement a function called
(sat-n? pred n lst)that returns#tif exactly n elements oflstsatisfypred, and#fotherwise.Implement a function called
(sat-count pred lst)that returns the total number of elements oflstthat satisfypred. As an extra constraint, there should be, at most, one call tosat-countin the body ofsat-count.Re-implement
some?,all?,none?, andsat-n?using a single call tosat-countfor each.Racket has a function called
(negate pred)that returns a new predicate that is the negation ofpred. For example(negate zero?)returns a new function that returns#twhen it is passed a non-zero input, and#fif it is passed 0.Implement your own version of
negate.Racket has a function called
(conjoin pred1 pred2)that returns a new predicate that behaves the same aspred1andpred2and-ed together.Implement your own version of
conjoin.Implement a function called
(conjoin-all plist), whereplistis a non-empty list of predicate functions, that returns a new predicate that is logically equivalent to the conjunction of all the predicates onplist.For example:
(define f (conjoin-all (list (lambda (n) (> n 0)) (lambda (n) (<= n 10)) odd?)))fis a predicate such that(f x)is true whenxis bigger than 0, less than or equal to 10, and odd. So(f 0)and(f 4)are false, and(f 1)and(f 7)are true.
Practice Haskell Programming Questions¶
Write a function called
isdigitthat tests if a character is one of the digits'0','1', …,'9'. It has this signature:isdigit :: Char -> Bool
Write a function called
isletterthat tests if a character is one of the 26 lowercase letters'a','b', …,'z', or one of the 26 uppercase letters'A','B', …,'Z'. It has this signature:isletter :: Char -> Bool
The following functions each take a predicate function and a list as input, and remove certain items from the list based on the predicate function. They all have this signature:
(a -> Bool) -> [a] -> [a]
ltrimremoves all items from the start of the list that satisfy the predicate function.rtrimremoves all items from the end of the list that satisfy the predicate function.trimremoves all items from both the start and end of the list that satisfy the predicate function.
Write the following two functions:
lpad c n lstreturns a new list equal tolstbut withncopies ofcon the left side. Write the most general type signature for it.For example:
> lpad '*' 5 "apple" "*****apple"
rpad c n lstreturns a new list equal tolstbut withncopies ofcon the right side. Write the most general type signature for it.For example:
> lpad '*' 5 "apple" "apple*****"
Practice Go Questions¶
Using English plus simple code examples, explain the difference between arrays and slices in Go.
Using English plus a simple code example, give a major reason why Go does not let you pass a slice to a function that expects an array.
Write a function called
vc_count(s string)that returns the number of vowels and consonants ins.A vowel is defined to be one of these lowercase letters:
a,e,i,o,u,y. A consonant is defined to be any character that is not a vowel, and also the lettery. Thusyshould be counted as both a vowel and a consonant.Also, you must use a ranged for-loop (in a sensible way) in your solution.
Your function must work with code like this:
v, c := vc_count("yay") fmt.Println(v, c)
Write a goroutine called
char_gen(s string, out chan rune)that returns the characters ofsone at a time on channelout. Use the built-in functioncloseto closeoutwhen all the characters have been sent.Demonstrate how to use
char_genby writing amainfunction that uses it to print out an entire string.Answer all the following questions with Go code.
Write a function called
add(x, y)that returns the sum of two ints, e.g.:fmt.Println(add(3, 5)); // 8
Write a function called
c_addthat is a curried version ofadd, e.g.:add3 := c_add(3) fmt.Println(add3(5)) // 8 fmt.Println(c_add(2)(4)) // 6Write a function called
curry(f)that returns a curried version off. Assume thatfis a (non-curried) function that takes two ints as input and returns an int.It can be used like this:
curr_add := curry(add) // add is the regular non-curried add function add2 := curr_add(2) fmt.Println(add2(6)) // 8
a) Write a function called
negpos(s)that takes a slice of ints as input, and returns two new slices, named neg and pos, where neg contains all the numbers insthat are less than 0, and pos contains all the numbers insthat are greater than, or equal to, 0. For example:s := []int{6, -2, 0, 2, -6, -4, 3} neg, pos := negpos(s) fmt.Println(neg) // {-2, -6, -4} fmt.Println(pos) // {6, 0, 2, 3}
Use features of Go to make
negposas short and clear as possible.b) To help test your
negposfunction, write a function calledall(s, pred)where:sis a slice ofintvaluespredis a function that takes oneintas input, and returns abool
all(s, pred)returnstrueif everyintinsis true forpred, andfalseotherwise (i.e. it returnsfalseifscontains 1, or more,int` values that cause ``predto returnfalse).c) Write a single Go statement of the form
all(s, pred)that returnstruejust when every element insis less than 0, andfalseotherwise.