LMU ☀️ CMSI 3801
LANGUAGES AND AUTOMATA I
HOMEWORK #1 PARTIAL ANSWERS

Code

Python

print("Hello, World!")

JavaScript

console.log("Hello, World!")

Lua

print("Hello, World!")

TypeScript

console.log("Hello, World!")

Haskell

main = putStrLn "Hello, World!"

Java

void main() {
    IO.println("Hello, World!");
}

Kotlin

fun main() {
    println("Hello, World!")
}

Swift

print("Hello, World!")

C

#include <stdio.h>
int main() {
    printf("Hello, World!\n");
    return 0;
}

C++

#include <iostream>
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Rust

fn main() {
    println!("Hello, World!");
}

Go

package main
import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Fortran

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

Julia

println("Hello, World!")

Ada

with Ada.Text_IO; use Ada.Text_IO;
procedure Hello is
begin
    Put_Line("Hello, World!");
end Hello;

Pyth

"Hello, World!

Exercises

  1. What is meant by filtering a list? Give an example that uses a built-in filter function in the language of your choice.

    Answer: Filtering means to select those elements from a list that satisfy a given predicate. Usually, a filter operation creates a new list that contains only those elements, but it is also possible to filter in place, modifying the original list by dropping the elements not satisfying the predicate. Here is an example in JavaScript:

    numbers.filter(n => n % 2 === 0)
    
  2. Give a compact Julia expression for the array just like the array called numbers except that every value is cubed. Use broadcasting.

    Answer: numbers .^ 3

  3. What is meant by the phrase “pragmatics of programming languages”?

    Answer: Pragmatics encompasses the practical aspects of programming language usage, including readability, writability, efficiency, the contexts in which languages are used, issues in language design, implementation, and performance, and the trade-offs involved in choosing one language over another for specific tasks.

  4. Explain, in detail, this fragment of K: {+/x[&x!2]^2}

    Answer: The expression is enclosed in curly braces, so it is a function. The parameter is x, because the first parameter to a function is K is always called x. Working from the inside out:

    • !2 maps the “mod 2” operation over the elements of x, returning a new array (which will contain 1s and 0s)
    • & returns the indices where the 1s appear in x!2, that is, an array containing the indexes of the odd elements of x.
    • [] selects the elements of x at the positions in &x!2.
    • ^2 squares each element of that new array.
    • +/ sums the elements of that squared array.

    Thus, this K fragment is a function computes the sum of squares of its odd elements.

    Here is a console session that demonstrates this:

      a: 3 5 4 2 1 8 9 0
      a!2
    1 1 0 0 1 0 1 0
      &a!2
    0 1 4 6
      a[&a!2]
    3 5 1 9
      a[&a!2]^2
    9 25 1 81.0
      +/a[&a!2]^2  
    116.0
    
  5. What does the term object-orientation mean today? What did it originally mean?

    Answer: Today, object-orientation is a programming paradigm in which objects encapsulate properties and dynamically-polymorphic methods and are organized into type hierarchies. Originally it referred to a system design in which objects communicated solely through messaging. Alan Kay, who coined the term, famously said: “OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things.”

  6. What characters comprise the following string: ᐊᐃᓐᖓᐃ? List the code point and name of each character that appears, in order. What is the meaning of the string, assuming its intended context?

    Answer: I used Java for this!

    jshell> "ᐊᐃᓐᖓᐃ".codePoints().forEach(c -> IO.println(String.format("U+%04X\t%c\t%s", c, c, Character.getName(c))))
    U+140A	ᐊ	CANADIAN SYLLABICS A
    U+1403	ᐃ	CANADIAN SYLLABICS I
    U+14D0	ᓐ	CANADIAN SYLLABICS N
    U+1593	ᖓ	CANADIAN SYLLABICS NGA
    U+1403	ᐃ	CANADIAN SYLLABICS I
    

    In Inuktitut, the phrase is a general greeting roughly translated as “Hello.”

  7. What is the difference between control flow and concurrency?

    Answer: Control flow describes activity within a single line of execution, while concurrency is about the coordination of multiple lines of execution.

  8. How do machine and assembly languages differ? Give an example that is different from the one seen in class.

    Answer: A machine language is a sequence of numbers, each representing a machine instruction. An assembly language is an alphanumeric representation of these instructions, together with features such as named variables and labels, and directives that give some overall structure to a program.

    Here is a simple example for the ARM 64 processor for a function that squares a 64-bit integer. The machine language (in hex) is:

    00 7C 00 1B C0 03 5F D6
    

    This corresponds to the assembly language:

    square:
        mul w0, w0, w0
        ret
    
  9. We saw, in class, a function that computed either $3n+1$ or $4n-3$ depending on whether $n$ is even or odd. Write this function in a programming language not seen in class.

    Answer: Here is the function in Mojo:

    fn f(n: Int) -> Int:
        (3 * n + 1) if n % 2 == 0 else (4 * n - 3)
    
  10. The language Verse is billed as a functional-logic programming languages. Write a short paragraph about Verse, including its creator, year of creation, why it was created, and what exactly “functional-logic” means.

    Answer: Verse was created at Epic Games and first released in 2023. It was created, as Gemini says, “to build the metaverse by providing a simple, powerful, and scalable tool for creating interactive 3D experiences within a decentralized, open economy, enabling developers to collaborate, monetize their creations, and integrate content and code seamlessly in shared, real-time environments.” The “functional-logic” paradigm, for which Verse touts itself as supporting, combines the declarative nature of logic programming with the higher-order functions and immutability of functional programming.