Skip to content

Instantly share code, notes, and snippets.

@jki127
Last active December 19, 2019 18:34
Show Gist options
  • Save jki127/a1ff762d30272121d66820ddd42964aa to your computer and use it in GitHub Desktop.
Save jki127/a1ff762d30272121d66820ddd42964aa to your computer and use it in GitHub Desktop.

Final Review - Programming Languages - Dec 12th, 2019

Table of Contents

Things to know

Smalltalk

  • do:
  • how to write BlockClosure with parameters
  • isEmpty
  • ifTrue
  • Order of Operations for methods:
    1. Unary
    2. Binary
    3. Keyword operators

Mark and Sweep algorithm

Possible Questions:

  • explain the algorithm
  • write the algorithm in a language of your choice
  • list the disadvantages of the algorithm

Prolog: Spatial vs Temporal

  • Spatial: returning all results at once
    • think of heads and tails logic
  • Temporal: return results one-by-one (semi-colon)

Exceptions

Possible Questions:

  • Describe the mechanism when a exception is executed
  • Here's some code with exceptions, what does it print?
  • Explain longjmp or setjmp

Smart Pointers

  • Construct programs
  • Alternatives to smart pointers
  • Look at an implementation smart pointer class and spot bugs

Haskell operators

  • element : list
  • list1 ++ list2

mapM vs map

mapM is used to apply a monadic operations to elements in a list. If we ran the following code with regular map then we will just get a list of IO () because map doesn't actually execute the monadic operation.

mapM putStrLn ["hello", "there", "noodle"]

Morphs from Pharo

  • Will not need to write code
  • Should know the basics

Javascript Prototype Inheritance

  • Be familiar with the inheritance model
  • How are inheritance hierarchies created?
  • How are methods evaluated?

Review Sheet

Question 1 - Monad

Answer:
* `foo` returns `Just 7`
* `bar 1 2` returns `Nothing`
* `baz 5` returns `Nothing`
	- We stop immediately when `bar c 1` returns Nothing!

Question 2 - Smalltalk Powerset

addToAll: val
	self do: [:subset | subset add: val. ].
	^self.

powerset
	|mysubsets|

	self isEmpty
		ifTrue:
			[self class newFrom: {self class new}].
		ifFalse:
			[
				mysubsets := (self allButFirst) powerset.
				mysubsets, mysubsets copy addtoAll: self first.
			].

Question 3 - uniq Prolog Spatial v Temporal

Spatial

uniq([], []).
uniq([H|T], [H|T2]) :- not(member(H,T)), uniq(T,T2).
uniq([H|T], T2) :- member(H,T), !, uniq(T, T2).

% Without a cut, like below, this would call return
% twice if an element is in a list three times
% uniq([H|T], T2) :- member(H,T), uniq(T, T2).

Temporal

uniq2([H|T], H) :- not(member(H,T)).
uniq2([_|T], X) :- uniq2(T, X).

% We don't need to write the base case explicitly,
% like below, because prolog fails non-existant
% predicates by default
% uniq2([],_) :- fail.

Other Questions

Haskell

What is the type of the following?

map reverse

reverse has the type [a] -> [a]. map has the type (a->b) -> [a] -> [b]

So that means map reverse is a function that takes in and returns a list of lists.

map reverse :: [[a]] -> [[a]]

Directed Graph

data Node a = Node a [Node a]

graph1 = Node 1 [
				Node 2 [Node 5[]]
				Node 3 [],
				Node 4 [Node 6 [Node7 [], Node 8 []]]
				]

graph 2 = Node 1 [
				Node 2 [Node 5 []],
				Node 3 [],
				Node 4 [Node 6 [Node 7 [graph2], Node 8 []]]
				]
hasCycles :: Eq a => Node a -> Bool
hasCycles node = go [] node
	where go seen (Node val outgoing) = 
		case val `elem` seen of
			True -> True
			False -> 
				let newseen = val : seen
				 in True `elem` (map (go newseen) outgoing)

#school/f19/proglang-f19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment