Haskell: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
=Introduction= | =Introduction= | ||
This is my first dip into Haskell, just felt like trying it | This is my first dip into Haskell, just felt like trying it. Haskell is a purely functional programming language. rather than a declarative language. I.E. it is a declarative language rather than imperative. With declarative, you tell the language what the thing is rather than how to do the thing. | ||
=Features= | |||
==Lazy== | |||
Haskell is a programming lazy language and only evaluates on usage. E.g. only evaluates the first 10 | |||
<syntaxhighlight lang="hs"> | |||
let infiniteList = [1..] | |||
take 10 infiniteList | |||
</syntaxhighlight> | |||
==Modules== | |||
Starting to look a lot like Lisp. This next bit shows how you can combine various functions of your own to produce and output | |||
<syntaxhighlight lang="hs"> | |||
let double = x = x * 2 | |||
let increment x = x + 1 | |||
let doubleThenIncrement x = increment(double x) | |||
doubleThenIncrement 10 // 21 | |||
</syntaxhighlight> | |||
==Statically Typed== | |||
With Haskell types are inferred | |||
=Hello World= | =Hello World= | ||
Here we start with main and strange format but no curly brackets which is good. | Here we start with main and strange format but no curly brackets which is good. | ||
Line 6: | Line 23: | ||
main :: IO() | main :: IO() | ||
main = putStrLn "Hello World" | main = putStrLn "Hello World" | ||
</syntaxhighlight> | |||
We build like gcc using | |||
<syntaxhighlight lang="bash"> | |||
ghc hello.hs -o hello | |||
</syntaxhighlight> | |||
=Date Models= | |||
==Types== | |||
*Numbers e.g. 1 | |||
*Chars e.g. "S" | |||
*Boolean e.g. True, False | |||
*List e.g. [1,2,3], [True, False, True] | |||
==List Comprehension== | |||
You can define a function and operate against a list like this | |||
<syntaxhighlight lang="bash"> | |||
[x*2 | x <-[1..10]] // [2,4,6,8,10,12,14,16,18,20] | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 21:46, 10 February 2025
Introduction
This is my first dip into Haskell, just felt like trying it. Haskell is a purely functional programming language. rather than a declarative language. I.E. it is a declarative language rather than imperative. With declarative, you tell the language what the thing is rather than how to do the thing.
Features
Lazy
Haskell is a programming lazy language and only evaluates on usage. E.g. only evaluates the first 10
let infiniteList = [1..]
take 10 infiniteList
Modules
Starting to look a lot like Lisp. This next bit shows how you can combine various functions of your own to produce and output
let double = x = x * 2
let increment x = x + 1
let doubleThenIncrement x = increment(double x)
doubleThenIncrement 10 // 21
Statically Typed
With Haskell types are inferred
Hello World
Here we start with main and strange format but no curly brackets which is good.
main :: IO()
main = putStrLn "Hello World"
We build like gcc using
ghc hello.hs -o hello
Date Models
Types
- Numbers e.g. 1
- Chars e.g. "S"
- Boolean e.g. True, False
- List e.g. [1,2,3], [True, False, True]
List Comprehension
You can define a function and operate against a list like this
[x*2 | x <-[1..10]] // [2,4,6,8,10,12,14,16,18,20]