Haskell: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 37: | Line 37: | ||
You can define a function and operate against a list like this | You can define a function and operate against a list like this | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
[x*2 | x <-[1..10]] | [x*2 | x <-[1..10]] -- [2,4,6,8,10,12,14,16,18,20] | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 21:47, 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]