Skip to content

Instantly share code, notes, and snippets.

@jki127
Last active December 5, 2019 21:45
Show Gist options
  • Save jki127/005cc1cdf63cf2fc8f4bc967c674d136 to your computer and use it in GitHub Desktop.
Save jki127/005cc1cdf63cf2fc8f4bc967c674d136 to your computer and use it in GitHub Desktop.

Student Presentations II & Final Overview - Programming Languages - Dec 5th, 2019

Table of Contents

PyPy

CPython

Python uses CPython to compile python to intermediate bytecode

What is PyPy?

PyPy is a drop-in replacement for CPython. PyPy uses a Just-In-Time (JIT) Compiler.

PyPy is 4.4 times faster than CPython on average.

Caching

On every run, CPython recreates bytecode. PyPy caches the native code produced which makes it faster on consecutive runs.

Limitations

  • Recompiles every run (not a compiler though)
  • Not compatible with some C extensions
  • Works better with longer running code
sum = 0
for i in range(10000000000):
	sum+=1

print(sum)
from datetime import date
from datetime import datetime

for i in range(9000000)
	datetime.now()

d = date.today()

print("today's date is ", d)

Fortran

Intro

  • Name means "Formula Translator"
  • Originally done on punchcards (sheets of paper)
  • First high-level language
  • Many aspects of the language reflect the limitations of the time like memory and processing speed being limited

Basic Data Types

  • Integer
  • Real
  • Complex
  • Logical
  • Character

Hello World

program hello
implicit none
	print *, "Hello World!"
end program hello

Implicit inbhits a feature of Fortran that assumes that any variables named i j k n or p is assumed to be an integer

program

Recursion

Recursion was only introduced to Fortran recently.

Memory locations were allocated before the program is run

recursive subroutine fibonacci(n, fib)
	implicit none
	integer, intent(in) :: n
	integer, intent(out) :: fib
	integer :: temp
	if (n <= 2) then
		fib = 1
	else
		call fibonacci(n-1, fib)
		...
		...

kind

kind is a new keyword that allows you specify how big (in bytes) you want your integers to be.

integer(kind=2) :: int1
integer(kind=4) :: int2
integer(kind=16) :: int3

Fortran's use today

  • Supercomputers
  • runs as fast as C++
  • 60 years of libraries to build off
  • Has adapted to become more like modern languages

Akka

  • Actor-based system written in Scala

Why do we need Akka?

Person A Person B

balance $300 balance $300 withdraw: 100 withdraw: 100

Instead of getting deadlocks, we can use Actors.

Actors

  • send messages to another actor

  • creates new actors

  • decides how to respond to the next message

  • It doesn't matter if each actor is running from different computers

Fault Tolerance

  • If an actors crashes, a supervisor can reset the actor to a previous state so it can continue running

PostScript

Page Description Languages

  • Describes the appearance of text and graphics in a device-independent manner
  • When send a PDF to print, it produces a PostScript file

Basics

  • Stack-based language
    • Helps with memory efficiency
    • operand stack
    • dictionary stack
    • execution stack (call stack)
  • Procedures
    • objects that may be exceuted in sequence
  • Operators
    • sub, add, dup, neg, div, mul
  • Arithmetic
    • postfix notation
      • 6 24 8 div add = 6 + (24/8)
72 72 scale

newpath
	2 1 moveto
	2 5 lineto
stroke
showpage

stroke

showpage

Arc

  • We use arc to draw a circle
4 4 scale

15 15 6 0 360 arc

Procedure

Let's create a square procedure

40 40 scale

5 5 translate

/newSquare {
	newpath
	0 0 moveto
	0 1 rlineto % Up
	1 0 rline to % Down
	0 -1 rlineto % Left
	-1 0 rlineto % Right
	1 0 0 setrgbcolor % Change color
} def

1 0 0 newSquare

fill % fill the shape color

Fibonacci

/n def 3

/fib {
	dup 1 gt {
		1 sub dup
		1 sub fib
		exch fib add
	} if

}def

Logo

  • Designed to introduce programming to new programmers
  • Turtle-based graphics
    • Python now has turtle-based graphics which it stole from Logo
  • MIT StarLogo
  • Northwestern's NetLogo
    • We'll be using NetLogo

Patches

Patches is like a 2d-array that refers to spaces on the main GUI

Basic Drawing

to setup
	clear-all
	create-turtles 5
end

We have to create a button inside the netlogo interface and connecet it to the setup function we just created.

to go
	fd 1
end

When we create a button for our go function we have to make sure that it runs it inside the context of turtles, not the "world".

It's not common to use the turtles as the context so instead we can change our go function like so:

to go
	ask turtles [fd 1]
end

Conway's Game of Life

breed [cells cell]
breed [babies baby]

patches-own [live-neighbors]


to setup
	clear-all
	set-default-shape cells "circle"
	set-default-shape babies "dot"
	ask patches [set live-neighbors]
	ask patches
		[if random-float 100.0 < 47
			[sprout-babies 1 [set color red]]]
	;go 
end

to go
	ask cells with [color = grey] [die]
	ask babies[
		set breed cells
		set color white
	]
	ask patches [
		ask neighbors
		[set live-neighbors live-neighbors + 1] ]

	ask patches
		[if not any? cells-here and live-neighbors = 3
		]

end

Final Exam Overview

  • Monads
    • Given a monad, write code using it
    • Given code using a monad, say what it does or what it prints
      • ex: the List monad from the monad homework
  • Smalltalk
  • Garbage Collection
    • Mark and Sweep
  • Smart Pointers
  • JavaScript OOP
    • Prototype-based inheritance
    • Given JS code, explain the inheritance model
    • prototype vs __proto__
  • Prolog
    • Temporal vs Spatial

CLP(FD) is not on the exam.

Prolog Example - Farmer, Cabbage, Wolf, Goat

Farmer, Cabbage, Wolf, Goat

  • Everyone needs to cross the river, but the boat can only fit two people.
  • If you leave the Goat with the cabbage unattended, it will eat the cabbage.
  • If you leave the Wolf with the Goat unattended, it will eat the Goat.

Initial state:

w = west
e = east
[Farmer, Wolf, Goat, Cabbage]

[w,w,w,w]

Goal state:

[e,e,e,e]
travel(e,w).
travel(w,e).

move([X,X,Goat,Cabbage],object,[Y,Y,Goat,Cabbage]) :- travel(X,Y).

move([X,Wolf,X,Cabbage],object,[Y,Wolf,Y,Cabbage]) :- travel(X,Y).
move([X,Wolf,Goat,X],object,[Y,Wolf,Goat,Y]) :- travel(X,Y).
move([X,Wolf,Goat,Cabbage],nothing,[Y,Wolf,Goat,Cabbage]) :- travel(X,Y).

safe([X,_,X,_]).
safe([X,X,_,X]).

solve([e,e,e,e], []).
solve(State, [FirstMove|OtherMoves]) :-
	move(State, FirstMove, NextState),
	safe(NextState),
	solve(NextState, OtherMoves).

Use length to limit the number of moves that can be used otherwise the farmer can keep going back and forth.

?- length(X,7), solve([w,w,w,w], X).

#school/f19/proglang-f19

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