How does Java 10 compare to Scala

Haskell in the subsequent comparison with Java

Table of Contents


Part 1: programming languages
1. Haskell
1.1. General
1.2. Programming in Haskell
1.2.1 Data types
1.2.2 Type - synonyms
1.2.3 Basics of the function
1.2.4 Type classes
1.2.5 List
1.2.6 Byte string
1.2.7 Tuples
1.2.8 If - else, guards and local functions
1.2.9 Higher order functions
1.2.10 Curry
1.2.11 Pattern Matching
1.2.12 recursion
1.2.12 modules
1.2.13 Input and output
1.2.14 Exceptions

2. General information about Java

Part 2: Comparison 13+




On the following pages I will go into the essential components of the Haskell programming language and briefly illustrate them using examples. If necessary, I will go into similarities with the Java language and specifically contrast both languages ​​at the end.

Part 1: programming languages

1. Haskell

1.1 General

Haskell is a purely functional programming language from 1990 named after the mathematician and logician "Haskell Brooks Curry". Like most functional programming languages, Haskell is based on the lambda calculus, which is why the lambda symbol was also defined as the logo.7 Initially, Miranda was supposed to serve as the basis, but the developers were unwilling to release the source code4. In contrast to imperative programming languages, a Haskell program is a single chain of expressions that delivers a result value after evaluation. So it consists exclusively of functions. According to the lambda calculus, any expressions can be simplified through "reductions" down to the elementary expressions4. Expressions are any pieces of code that return a value. In addition, Haskell is strongly statically typed, which means that exactly one type is assigned to each expression, which is already known when it is compiled. This bypasses possible errors. This can also be done through "type interference" - Haskell can therefore infer a type himself, so that you do not have to determine it yourself. Since the return is always the same when the same arguments are entered, the result value depends solely on the parameters. So there are no state variables. Haskell influenced Python and Scala, among others.

1.2 Programming in Haskell

1.2.1 Data types

The data types for natural numbers in Haskell are Int (limited size) and Integer (unlimited size). For point numbers, these are float and double (larger). Just like in Java there is Bool with the truth values ​​True and False and Char and String for the representation of letters and words.

To define your own data types, use the data command:

data = | | ...

Value constructors are functions, which is why they can also get parameters: data form = circle float | Rectangle Float Float | Square Float They should not be compared to the constructors in the Java programming language, in which they are used to initialize an object; you define different structures of the same data type. A commonly used data type is Maybe a, predefined as: data Maybe a = Nothing | Just a It is used when the return value cannot be anything, i.e. Nothing. Otherwise it is Just .

If you want to make it clearer which type stands for what or if you want to access the individual elements, the "record syntax" is very helpful:

data teacher = teacher {first name :: string,

surname :: String,

fach :: String} deriving (Show, Read)

Due to deriving (Show, Read), the type teacher is an instance of the type class Show and Read. Since teacher is now part of these classes, it can be represented as a string with show and converted from its string representation back into a data type with read.

meinLehrer = teacher {first name = "unknown", last name = "Ortmann", subject = "computer science"}

1.2.2 Type - synonyms

Type-synonymous defines String as a list of Chars (type String = [Char]).

You do not create a new type with it, but only a synonym for existing types in order to make the code more readable. If you were to work a lot with vectors or pairs (2-tuples), you could define them as follows:

Figure not included in this excerpt

1.2.3 Basics of the function

Functions are defined by equations and parameters are separated from one another by spaces: add [illustration not included in this reading sample]

It doesn't matter in which order functions are defined. Since Haskell is purely functional, operators like (+) are also such.

The syntax of a function signature is as follows:

Figure not included in this excerpt

a = type variable of the type class Num. Functions with type variables are called polymorphic functions, those without them are called monomorphic. The type class Num restricts a to numbers. Without type variables, you could also write it like this: "add :: Int -> Int-> Int". Then the function could only be called with numbers of the type Int.

Two-digit functions can not only be written in prefix notation, but also in infix notation. E.g. add 2 3 can also 'add' as 2

3 can be written. In Haskell, functional application binds the most: (head [3,4]) + (max 3 42) == head [3, 4] + max 3 42

“+” Has a lower precedence value than head and max (values ​​are given from 1 to 9).


End of the reading sample from 15 pages