Proposed sequence of labs about the calculator for course 23/24
The PL labs of this group:
while -> strings -> left-side -> array-maps-and-null
corresponds to the branches
- while
- strings
- left-side
- array-map
of the repo functions-solution (opens in a new tab)
➜ functions-solution git:(left-side) git remote -v
origin git@github.com:ULL-ESIT-PL/functions-solution.git (fetch)
origin git@github.com:ULL-ESIT-PL/functions-solution.git (push)
pcgull-solution https://github.com/ULL-ESIT-PL-2223/arith2js-parallel-computing-group-parallel.git (fetch)
pcgull-solution https://github.com/ULL-ESIT-PL-2223/arith2js-parallel-computing-group-parallel.git (push)
scope-intro-solution git@github.com:ULL-ESIT-PL/calc2js-solution.git (fetch)
scope-intro-solution git@github.com:ULL-ESIT-PL/calc2js-solution.git (push)
upstream https://github.com/ULL-ESIT-PL/arith2js-solution.git (fetch)
upstream https://github.com/ULL-ESIT-PL/arith2js-solution.git (push)
at
➜ functions-solution git:(left-side) pwd -P
/Users/casianorodriguezleon/campus-virtual/2223/pl2223/practicas/functions/functions-solution
Regrets
Don't know how to deal with this
Goal: Functions on the left side of an assignment
In the calculator language, the left side of an assignment only an ID is allowed. We want to extend the language to allow the modification of functions.
Introduction
We want to extend the language so that on the left side of an assignment you can have a function modification. For example, the following code should be valid:
f = fun(x) { x + 1 },
f(0+2) = 8, # Constant folding lab for the future 0+1, 1*8
f(1+3) = 1000,
write(f(0)), # 1
write(f(2)), # 8
write(f(4)) # 1000
Goal: Array notation for functions and otherwise
The notation
a = [4;7;9 ... 0]
defines a
as a function such that a(0)
returns 4
, a(1)
returns 7
, and a(2)
returns 9
and otherwise
returns 0
.
If the ... expression
is not present, the function returns an special object called
null
for any argument not explicitly defined.
a = [4+2;5+3i;9-i],
print(a(0)), # { re: 6, im: 0 }
a(1) = 333,
print(a(1)), # { re: 333, im: 0 }
print(a(9)), # null
print(a) # { re: 6, im: 0 }, { re: 333, im: 0 }, { re: 9, im: -1 } ]
The former code translates to
➜ functions-solution git:(array-map) ✗ bin/calc2js.mjs test/data/input/arr.calc | npx prettier --parser babel
#!/usr/bin/env node
const {
arr,
Complex,
print,
memoize
} = require('/Users/casianorodriguezleon/campus-virtual/2223/pl2223/practicas/functions/functions-solution/src/support-lib.js');
/* End of support code */
let $a;
((((($a = arr(
Complex('4').add(Complex('2')),
Complex('5').add(Complex('3i')),
Complex('9').sub(Complex('i'))
)),
print($a(Complex('0')))),
($a = memoize($a, [Complex('1')], Complex('333'), 0))),
print($a(Complex('1')))),
print($a(Complex('9')))),
print($a);
Hashes/Maps/Objects
Proposed syntax for hashes/maps:
a = { 4: 5; 7: 3i; "hello": 6 ... 0}
meaning a function that a(4) = 5
, a(7) = 3i
, a("hello") = 6
and otherwise a(x) = 0
for all x
.
An alternative to explore/study is to consider the introduction of the meth
keyword that will define a functions that usea the "object" as a context. For example, the following code:
b = { "x": 5; "add": meth(z) { x + z} },
f("add")(3) # 8
null object
Introduce null
as an extension of 0
and the empty string ""
so that it holds the following properties:
null + 4 == 4,
null + "hello" == "hello", # this is not the JS behavior
null * 4 == 0,
4 - null == 4,
fun(x) { x + 1 } * null # is the same as fun(x) { 0 },
null && true == false,
Notice that if a
is a function like in the former example a * null
is the function a(x) = 0
for any x
.