













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!














:: operator - ANScalled the cons operator, used to construct a new list by adding an elm to the front of an existing list 8 major categories of control flow - ANS1. sequential
associativity - ANSthe order in which operators of the same precedence are evaluated in the absence of parentheses boxing - ANSthe process of converting a value type to a reference type (unboxing: the reverse, converting a reference type to a value type) C language - ANShigh-level assembler, abstracts you away from the implementation details of any particular CPU but allows for programming that is still close to the "bare metal" of the machine. It is almost impossible to understand how operating systems work without understanding the C programming language car - ANSreturns the first elm of a list cdr - ANSreturns the rest of the list (all elms but first) constructors - ANSfunctions used to create and initialize objects or instances of a class in object-oriented programming control abstraction - ANSexpressing control flow structures in a way that hides low-level details from the programmer. achieved through higher-order functions currying - ANStechnique where a function that takes multiple arguments is transformed into a sequence of functions, each taking a single argument. in functional programming, a function that appears to take multiple parameters is often curried. when you provide fewer arguments than the function expects, it returns a new function that expects the remaining arguments.
let divide x y = if y = 0 then None else Some (x / y) record type: type person = { name: string; age: int } let john = { name = "John"; age = 30 } list comprehension: let evens = [for x in [1; 2; 3; 4; 5] do if x % 2 = 0 then yield x] functional programming - ANSformal definition: programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data digestible definition: coding style where functions are first-class citizens, which means they can be assigned to variables, passed as arguments, and returned as values. emphasizes immutability, pure functions, and declarative programming. functional programming languages include - ANSscala, f#, Ocaml, SML golang language - ANS Haskell - ANSfunctional programming language, uses lazy evaluation by default, strong emphasis on immutability, referential transparency, and type safety high-order functions - ANSfunctions that can take other functions as arguments or return functions as results
how to evaluate a function's type - ANS1. find the input type
| _ -> "Other" list creation: let numbers = [1; 2; 3; 4; 5];; recursion: let rec factorial n = if n <= 1 then 1 else n * factorial (n - 1);; higher-order functions: let square_all lst = List.map (fun x -> x * x) lst;; option type: let divide x y = if y = 0 then None else Some (x / y);; record type: type person = { name: string; age: int };; let john = { name = "John"; age = 30 };; list comprehension: let evens = List.filter (fun x -> x mod 2 = 0) [1; 2; 3; 4; 5];; (* Lambda function that squares its argument *) let square = fun x -> x * x;; optionals - ANSfeature designed to handle the absence of a value orthagonality - ANSthe property that a relatively small set of primitive constructs can be combined in a relatively small number of ways to build the control and data structures of a language
An orthogonal language has features that can be freely combined without restrictions pattern matching - ANSfeature in functional programming languages that allows you to destructure and match complex data structures, such as lists. It simplifies code by handling different cases based on the structure of the data Ex: (define (classify-value x) (match x ((? number?) 'numeric) ((? string?) 'text) (else 'unknown))) polymorphism - ANSthe ability of a variable, function, or operator to take on multiple forms or behaviors precedence - ANScrucial for determining the order in which different operations or statements are executed programming paradigm - ANSapproach to programming that guides the way developers design, structure, and organize their code Prolog - ANSdeclarative programming language designed for tasks involving rule-based logic programming (uses logical paradigm) prolog syntax - ANS% rules for factorial using recursion factorial(0, 1). factorial(N, Result) :- N > 0,
stack: function call management and local variables rust - ANSsystems programming language known for its focus on memory safety, zero-cost abstractions, and concurrency support Scala - ANSstatically typed, functional programming language that runs on the Java Virtual Machine (JVM) and combines object-oriented and functional programming paradigms features concise syntax, pattern matching, and immutable collections Scheme - ANSLanuage of Lisp dialect, uses prefix notation where the operator precedes its operands scheme syntax - ANS; Define (for binding values) (define x 5) ; Let (for local bindings) (define result (let ((y 10) (z 20)) (+ x y z))) ; Lambda (for defining anonymous functions) (define square (lambda (a) (* a a))) ; Car and Cdr (for accessing elements of a list) (define myList '(1 2 3)) (define firstElement (car myList)) (define restOfList (cdr myList)) ; Cons (for constructing lists)
(define newList (cons 0 myList)) ; Null? (for checking if a list is empty) (define isListEmpty (null? '())) ; If (for conditional branching) (define (abs-value x) (if (< x 0) (- x) x)) scripting languages - ANSmuch slower in execution speed than Java or C, elaborate functionality in just a few lines of code python, perl, ruby, groovy short-circuit evaluation - ANSprogramming language feature where the evaluation of a logical expression is stopped as soon as the result is determined SML (ML) syntax - ANS(* Binding Values ) val x = 10; ( Function Definition ) fun add(x, y) = x + y; ( Pattern Matching ) fun factorial 0 = 1 | factorial n = n * factorial (n - 1); ( Conditional Expression ) fun absolute x = if x < 0 then ~x else x; ( Tuples *)
statically typed - ANSvariable types are checked at compile-time, reducing the likelihood of runtime errors structured flow vs unstructured flow - ANSstructured flow: involves organized, modular programming constructs unstructured flow: lacks such organization and can lead to spaghetti code structuring and destructuring - ANSorganizing and breaking down complex data structures into simpler components Swift - ANSstatically-typed programming language developed by Apple for app development, known for its type safety, modern syntax, optionals, and emphasis on performance tail recursion - ANSform of recursion where the recursive call is the last operation in the function test question (multiple choice): given the OCaml/F# function: let rec f = function | [] -> [] | x :: xs -> (x, x+1) :: f xs. What does the following method call yield: f [2; 4; 6]? a) [] b) [12] c) [4; 8; 12]
d) [(2,3)(4,5)(6,7)] - ANSfunction: used to pattern match on the input argument [] -> []: matches an empty list x :: xs -> (x, x+1) :: f xs: matches a non-empty list with a head element x and a tail xs. It then creates a tuple (x, x+1) and appends it to the result of the recursive call f xs.
f) exception handling - ANSvariable declaration is related to variable and data type handling, not directly a category of control flow test question (T/F): if a language uses "normal order evaluation," it's possible that g(x) may never be evaluated in an expression such as in result = f(x, g(x)). - ANStrue, lazy evaluation delays the evaluation of an expression until its value is actually needed. If the value of g(x) is not used in the computation of result, it may never be evaluated. Haskell, a functional programming language, uses lazy evaluation by default. test question (T/F): in functional programming languages like OCaml/F#, a function requiring more than one parameter will accept only one parameter and yield a new function to accept the next parameter - ANSTrue test question (T/F): in languages that implement tail recursion, it is generally best to avoid recursion altogether for better performance and to avoid potential stack overflows - ANStrue, some programming languages optimize tail-recursive calls by reusing the current function's stack frame, reducing the risk of a stack overflow. However, not all languages or compilers support this, and using iteration may be preferable for performance test question (T/F): in object-oriented languages such as Java, the compiler can verify that every variable is properly initialized to a non-null value before its first use in an expression. - ANSfalse, while some modern languages, including Java, require variables to be initialized before use, the compiler does not always check for null values. It is the responsibility of the programmer to ensure proper initialization to avoid null pointer exceptions during runtime test question (T/F): In Prolog, a function's formal parameters may serve as both "inputs" and "outputs." - ANStrue, formal parameters are often used bidirectionally. Predicates in Prolog can have input, output, or both input and output parameters. The values of parameters are determined by unification
test question (T/F): individual programming languages may be classified as either object-oriented or functional but never have features of both paradigms, as this would create ambiguities for compilers. - ANSfalse, many modern programming languages are multi-paradigm. Languages like Scala, Kotlin, and Swift are considered both object-oriented and functional test question (T/F): unlike the arithmetic operators in most programming languages, Scheme arithmetic operators do not have explicit precedence associated with them - ANStrue, in Scheme, a Lisp dialect, arithmetic expressions are typically written in prefix notation, and there is no need for explicit precedence rules. Instead, Scheme uses parentheses to indicate the order of operations. For example, instead of 3 + 5 * 2, you would write (+ 3 (* 5 2)). test question (T/F): whereas classes can be used to implement "type abstraction," in OO languages, high...functions can be used to implement "control abstraction" in functional languages - ANStrue test question: assume a "pseudo-Java/C" language that DOES NOT employ short-circuit evaluation of Boolean expressions. What would be the output of the following fragment upon invoking main( )? boolean fun1 () { println("Fun1"); return false; } boolean fun2 () { printIn("Fun2"); return true; } void main() {println( fun1() && fun2() ); } - ANSFun Fun false (both functions executed because lack of short-circuit evaluation, evaluates to false at the end because of the && test question: given the following set of Prolog facts/rules:
(starting) address of someArray is memory address 1000, what memory address refers to the element someArray [3] [2]? - ANSstarting address: 1000 size of each row: 10*2=20 bytes (for 10 integers) size of each elm: 2 bytes addr of someArray[3][2] = starting address + (row index * size of each row)
test question: write a function called valueRange in OCaml/F# which: