Haskell: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 14: Line 14:
let increment x = x + 1
let increment x = x + 1
let doubleThenIncrement x = increment(double x)
let doubleThenIncrement x = increment(double x)
doubleThenIncrement 10 // 21
doubleThenIncrement 10 -- Answer is 21
</syntaxhighlight>
</syntaxhighlight>
==Statically Typed==
==Statically Typed==
With Haskell types are inferred
With Haskell types are inferred

Revision as of 21:48, 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]

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]