User Tools

Site Tools


cs_lang:haskell

Presentation

Paradigm

  • purely-functional;
  • lazy evaluation.

Typing discipline

  • static typed;
  • strongly typed;
  • inferred.

Learning Haskell

Simple examples

You can compare functions below with the Objective Caml version.

Increment a number

let inc = (+) n
 
-- Alternative:
let inc n = succ n

Caesar cipher

-- Shifts a character to the right if positive, left if negative.
shift :: Int -> Char -> Char
shift n c | isUpper c = chr $ ord 'A' + ((ord c + n - ord 'A') `mod` 26)
          | isLower c = chr $ ord 'a' + ((ord c + n - ord 'a') `mod` 26)
          | otherwise = c
 
 
encode :: Int -> String -> String
encode _  [] = []
encode n  xs = map (shift n) xs

Parity

parity :: Integral a => [a] -> [Bool]
-- Using recursion
parity [] = []
parity (x:list) = [even x] ++ parity list
 
-- Using map
parity list = map even list
cs_lang/haskell.txt · Last modified: 2012/04/30 16:51 by cedric