






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







(multicons 'z 3 '(a b c)) => (z z z a b c) (multicons '(a b) 3 '(x y z)) => ((a b) (a b) (a b) x y z) (multicons 'z 0 '(a b c)) => (a b c)
(define (multicons x n xs) (if (zero? n) xs (cons x (multicons x (- n 1) xs))))
Or here is a nice tail-recursive solution. (Both would receive full credit though.)
(define (multicons x n xs) (if (zero? n) xs (multicons x (- n 1) (cons x xs))))
class Toy
def initialize(size, type) @size = size @type = type end
def goodness size end
end
(a) Add code to Toy to define public getters for size and type (but not setters), and to mix in the Comparable module. Write your extra code in the blank spaces in the Toy definition above. To compare two toys, compare their goodness. You should define in Toy the method needed by Comparable; this should then automatically let you compare two toys t1 and t2 using t1>t2, t1, and so on in Toy. Hint: 3<=>10 evaluates to -1.
class Toy include Comparable attr_reader :size, :type
def initialize(size, type) @size = size @type = type end
def goodness size end
def <=> other goodness <=> other.goodness end
end
(b) Write a subclass of Toy called StuffedAnimal.
indices 'b' "ababb" => [1,3,4] indices true [false,false,true] => [2] indices 'x' "abc" => []
indices x xs = helper x 0 xs
helper x n [] = [] helper x n (y:ys) = if x==y then n : helper x (n+1) ys else helper x (n+1) ys
indices :: (Num a, Eq t) => t -> [t] -> [a]
Or here is a version that doesn’t use a helper function:
indices x [] = []
indices x (y:ys) = if x==y then 0 : rest else rest where rest = map (+1) (indices x ys)
def test1(a,b) a["x"] = "squid" b["x"] = "clam" end def test2(a,b) a["x"] = "tuna" b = {"x"=>"starfish"} end
a = Hash.new test1(a,a) puts a test2(a,a) puts a
b = Hash.new c = Hash.new test1(b,c) puts b puts c test2(b,c) puts b puts c
{"x"=>"clam"}
{"x"=>"tuna"}
{"x"=>"squid"}
{"x"=>"clam"}
{"x"=>"tuna"}
{"x"=>"clam"}
index_of(X, [X|], 0). index_of(X, [|Xs], I) :- I > 0, I2 is I-1, index_of(X, Xs, I2).
(a) index_of(b,[a,b,c,d],3) => false (b) index_of(a,Xs,0) => Xs = [a|_1] (c) index_of(a,Xs,2) => Xs = [_2, 3, a|
index_of(X, [X|], 0). index_of(X, [|Xs], I) :- I#>0, J#=I-1, index_of(X, Xs, J).
(a) index_of(b,[a,b,c,d,a,b,c,d],N) => N= N= (b) index_of(X,[a,b,c],N) => X=a, N= X=b, N= X=c, N= (c) index_of(a,Xs,N) => Xs=[a|_1], N= Xs=[_2,a|_3], N= Xs=[_4,_5,a|_6], N=
...
(a) F Racket’s eq? function could be added to OCTOPUS as a new primitive function. Object identity is not meaningful for ordinary Haskell data, so adding eq? to OCTOPUS would require changing how OCTOPUS data is represented. For example, (equal? (cons 1 '()) (cons 1 '())) evaluates to #t in both Racket and OCTOPUS, as it should. What about (eq? (cons 1 '()) (cons 1 '()))? In Racket this evaluates to #f — getting this to also evaluate to #f in OCTOPUS would require changing the representation of OctoValue. (b) T Adding support for floating-point numbers to OCTOPUS would require changes to the lexer and/or parser, in addition to changes to the interpreter. (c) F The class Class in Ruby is a subclass of itself. (d) T The class Class in Ruby is an instance of itself. (e) F Any two Haskell lists can be tested for equality, since the list type is in the Eq type class. (Consider a list of functions, for example [sin,cos]==[sin,cos]. Two lists can be tested for equal- ity only if the elements are in the Eq type class.) (f) T Any let* expression in Racket can be rewritten as a set of nested let expressions. (g) F Adding a cut to a Prolog program may change the number of answers that are returned, but will never result in different answers. We ended up throwing out this question. The intended answer was F (consider removing the cut from the definition of not in the lecture notes, since with the cut you can get a fail and if no cut some answer) — but you could argue that is still just changing the number of answers. (h) F Java methods can be contravariant in the return type. (But they can be covariant in the return type.) (i) T Java methods can be overloaded based on the declared types of the method arguments. (j) F In Ruby, a singleton class has only one superclass, but other classes may have multiple superclasses.
class Book attr_reader :author, :title def initialize(author, title) @author = author @title = title end def description title + " by " + author + "." end end
class Textbook < Book attr_reader :subject def initialize(author, title, subject) super(author,title) @subject = subject end def description return super + " A textbook about " + subject + "." end
end
class AnonymouslyWrittenBook < Book def initialize(title) @title = title end def author "anonymous" end end
Suppose we make three objects b, t, and a by evaluating these statements:
b = Book.new("J.K. Rowling", "Harry Potter and the Deathly Hallows") t = Textbook.new("James Stewart", "Calculus", "mathematics") a = AnonymouslyWrittenBook.new("Haskell Good")
Then what is the result of evaluating each of these expressions? Hint: the instance_variables method returns an array of instance variable names, like this: [:@x, :@y].
b.description Harry Potter and the Deathly Hallows by J.K. Rowling.
t.description Calculus by James Stewart. A textbook about mathematics.
a.description Haskell Good by anonymous.
b.instance_variables [:@author, :@title]
t.instance_variables [:@author, :@title, :@subject]
a.instance_variables [:@title]
Note that since it doesn’t call the inherited initialize method, a doesn’t have an author instance variable.