Functional-Basic (FB for short) is a language that only allows numbers and lists. Basic features, such as the
if statement are missing in an attempt to simplify the language.
Everything in this language is immutable.
input is always assigned to the script input, which can be either a number or a list.
Note that you must support the order of operations for these. (https://en.wikipedia.org/wiki/Order_of_operations#Definition)
+: Addition - Applies to both numbers and lists.
-: Subtraction - Only applies numbers.
*: Multiplication - Only applies numbers.
/: Division - Only applies numbers.
^: Exponentiation - Only applies numbers.
Functions you must implement
sum: List of numbers -> NumberSum a list of numbers. Sum of
product: List of numbers -> NumberGet the product of a list. Product of
print: Anything -> [empty list]Print the value to standard out. Returns empty list.
Output format of the
The output should look like how it would be coded literally into the program, with the minor change that one space is required after a comma in lists. The values infinity and negative infinity are
Not a number is represented by
The syntax for FB is very simplistic. Below is a description of each major section of the syntax.
identifier : a string of only letters
Though I call them variables, they are really immutable. The only place where a space is required in the syntax is between the
names will never contain numbers, just upper and lower case ASCII letters. For simplicity you can assume that identifiers will never be used twice, even in
for loops. Also names will not overlap with built-ins (
for, ...). Syntax:
let [identifier] = [some expression][newline]
let sumOfLst=sum [1,2] let prod = product [2,2]
Numbers in FB conform to the IEEE 754/854 standard, with implementations providing a minimum of 32-bit precision.
-Infinity will not occur in the program source, but may be a result of a calculation.
Lists have a very limited set of capabilities. You can add lists together with
+, and you can map over them with
for. No indexed array access is supported here. Lists can have nested list inside them, but if it does it won't have any numbers as direct sub-children (i.e. lists don't mix types). List have only 2 ways to be created, the literal syntax and by mapping over a list with a
Syntax is just like most list types in other languages.
let b =  <-- not sure what you can do with a empty list... let c = [5, 6] let d = [5, ] <-- invalid let e = [[5, 2, 6], ] <-- valid let f =  + 5 <-- invalid let g =  +  <-- valid let h = [[6, 2], ] +  <-- valid let i = [[6, 2], ] +  <-- invalid
For loops in FB behave much like
map in other languages in that it returns a list with the function applied to each element of the list. For simplicity you will never have nested
for loops. Identifiers from outer scopes may be used in the expression. The expression will always be on the same line.
for ([identifier] in [list, may be an identifier pointing to a list]) [expression]
for(x in lst) x+1 let nList = for (bestNameEver in [2, 5, 6]) bestNameEver^2 let lstOfLsts = for (i in b) [b, b+1] for(x in [1, 2, 3])print x
Really only 3 functions exist in FB,
print sum  prints argument is sum. print (sum)  sum grabs the list since that is the next thing and evaluates it. print (5)
[function name][minimum one space][expression as the argument]
print [5, 1] sum [6, 7] print 1+2 <-- This is invalid, because print grabs the `1` and returns ``, and `+2` is invalid print (1+2) <-- This is valid
Other syntax features
Parenthesis are supported.
You can assume that all programs you will receive are syntactically correct and will not violate any rules placed on numbers and lists. The only thing that results in a error case is division by zero (
x/0). If you encounter a division by zero you simply halt execution of your program and print
Division by zero! to either stdout or stderr.
Code and output. (
> denotes code):
>print sum [1, 2, 3] 6 >print for (x in [1, 2, 3]) x+2 [3, 4, 5] >let aFloat = 4.2 >let aInt = 5 >let aList = [aFloat, aInt] >print product aList 21 >let x = for (y in [2.3, 5.6]) y/5*6 >print product x 18.5472 >let superSecretNumber = 0 >print superSecretNumber 0 Input: [2, 3, 4] >for (i in input) print i*2 4 6 8 >let answerToLife = 6*9 >print answerToLife 54 >print print sum [5, 2] 7  >let a = [1, 2, 3] >let b = for (x in a) [x, x+1] >print b [[1, 2], [2, 3], [3, 4]] >print print    >print for (i in [1, 2, 3]) print i 1 2 3 [, , ]