Haskell: Difference between revisions
Line 40: | Line 40: | ||
<syntaxhighlight lang="hs"> | <syntaxhighlight lang="hs"> | ||
[x*2 | x <-[1..10]] -- Answer is [2,4,6,8,10,12,14,16,18,20] | [x*2 | x <-[1..10]] -- Answer is [2,4,6,8,10,12,14,16,18,20] | ||
</syntaxhighlight> | |||
=Constructs= | |||
<syntaxhighlight lang="hs"> | |||
</syntaxhighlight> | |||
==Pattern Matching== | |||
No surprises, and I love pattern matching | |||
<syntaxhighlight lang="hs"> | |||
-- Pattern Matching | |||
coffeeType :: String -> String | |||
coffeeType "Epresso" = "Strong and bold" | |||
coffeeType "Cappuccino" = "Frothy and delicious" | |||
coffeeType "Filter" = "Watery and weak" | |||
coffeeType _ = "I have no idea what that is" | |||
main = do | |||
print(coffeeType "Epresso") | |||
print(coffeeType "Cappuccino") | |||
print(coffeeType "Filter") | |||
print(coffeeType "Latte") | |||
</syntaxhighlight> | |||
==Guards== | |||
<syntaxhighlight lang="hs"> | |||
</syntaxhighlight> | |||
==Where Clause== | |||
<syntaxhighlight lang="hs"> | |||
</syntaxhighlight> | |||
==Recursion== | |||
<syntaxhighlight lang="hs"> | |||
</syntaxhighlight> | |||
==Higher Order Functions== | |||
<syntaxhighlight lang="hs"> | |||
</syntaxhighlight> | |||
==Lambda Expressions== | |||
<syntaxhighlight lang="hs"> | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 22:07, 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 -- Answer is 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]
- Tuple e.g. (1,2,3, "string, True)
List Comprehension
You can define a function and operate against a list like this
[x*2 | x <-[1..10]] -- Answer is [2,4,6,8,10,12,14,16,18,20]
Constructs
Pattern Matching
No surprises, and I love pattern matching
-- Pattern Matching
coffeeType :: String -> String
coffeeType "Epresso" = "Strong and bold"
coffeeType "Cappuccino" = "Frothy and delicious"
coffeeType "Filter" = "Watery and weak"
coffeeType _ = "I have no idea what that is"
main = do
print(coffeeType "Epresso")
print(coffeeType "Cappuccino")
print(coffeeType "Filter")
print(coffeeType "Latte")