Haskell
![]() |
http://www.haskell.org |
similar languages: | ML Gofer Hope Objective Caml Miranda | |
Description: | Haskell is a pure lazy functional programming language:
|
Factorial (1) | Michael Neumann |
fac 0 = 1 fac n = n * fac (n-1) -- type "fac 6" to calculate the result |
Calculates the factorial. Results
720 . |
Factorial (2) | Michael Neumann |
fac n | n == 0 = 1 | otherwise = n * fac (n-1) -- type "fac 6" to calculate the result |
Calculates the factorial. Results
720 . |
Fibonacci | Mirko Rahn |
-- ersten zwanzig Fibonacci-Zahlen -- by Mirko Rahn <mai99dla@studserv.uni-leipzig.de> module Main where fibs = 1 : 1 : zipWith (+) fibs (tail fibs) main = print $ take 20 fibs -- Ausgabe: -- [1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765] |
Outputs the first 20 fibonacci
numbers. |
HaXml Demo Application | Michael Neumann |
{- Demo Application using HaXml 1.02 Copyright (c) 2001 by Michael Neumann $Id: haxml-demo.hs,v 1.1 2001/10/17 22:41:17 michael Exp $ HaXml: http://www.cs.york.ac.uk/fp/HaXml Compile with: hmake -nhc98 haxml-demo For sample XML file, see below. -} module Main where import XmlLib import XmlCombinators main = processXMLwith ( mkElem "HTML" [ mkElem "BODY" [ message `o` (tag "messages" /> tag "message") ] ] ) message = mkElem "DIV" [ -- Message Title mkElem "DIV" [ mkElem "B" [ ("["!) , tag "message" /> tag "category" /> txt , ("] "!) ] , mkElem "EM" [ txt `o` children `o` tag "title" `o` children `o` tag "message" ] , literal " at " , tag "message" /> tag "date" /> txt , literal " sent by " , tag "message" /> tag "author" ?> mkElemAttr "A" [ ("HREF", (tag "message" /> tag "author" /> tag "email" /> txt) ) ] [ tag "message" /> tag "author" /> tag "name" /> txt ] :> literal "Unknown Person" ] , -- Message Body mkElem "DIV" [ keep `o` (tag "message" /> tag "text") ] , mkElem "BR" [] ] {- Sample XML file follows: <?xml version="1.0"?> <messages> <message id="1"> <date>2001-12-31</date> <category>Admin</category> <title>CVS Updated</title> <text>CVS has been updated.</text> </message> <message id="2"> <author> <name>Michael</name> <email>mneumann@ntecs.de</email> </author> <date>2001-12-30</date> <category>Programming</category> <title>New function</title> <text>New method <font color="red">set_parser</font> in module XXX.</text> </message> </messages> -} |
Transforms a XML document. |
Hello World | Michael Neumann |
module HelloWorld (main) where main = putStr "Hello World\n" |
Prints "Hello World" onto the
screen. |
InsertionSort | Michael Neumann |
module InsertionSort (insertionSort) where import List (insert) insertionSort :: Ord a => [a] -> [a] insertionSort = foldl (\ a b -> insert b a) [] |
Sortieren durch Einfuegen |
InsertionSort (2) | Michael Neumann |
module InsertionSort (insertionSort) where -- insert is already defined in module List of Haskell 98 insert e [] = [e] insert e lst@(x:xs) | e < x = e : lst | otherwise = x : (insert e xs) insertionSort lst = insertionSort' lst [] where insertionSort' [] lst = lst insertionSort' (x:xs) lst = insertionSort' xs (insert x lst) |
Sortieren durch Einfuegen |
Quicksort | Michael Neumann |
quicksort [] = [] quicksort (s:xs) = quicksort [x|x <- xs,x < s] ++ [s] ++ quicksort [x|x <- xs,x >= s] |
Quicksort sorting algorithm
|
Squares (1) | Michael Neumann |
-- -- written in Haskell 1.4 (Hugs 1.4 for Windows) -- by Michael Neumann -- module Squares (main) where square :: [Int] -> String square [] = "\n" square (h:t) = show (h*h) ++ " " ++ square t main = putStr (square [1..10]) |
Outputs the squares from 1 to
10. |
Squares (2) | Matthias Mann |
-- -- von Matthias Mann -- module Squares (main) where import List main = putStr . concat . intersperse " " . map (show . (^2))) [1..10] |
Outputs the squares from 1 to
10. |
Squares (3) | Michael Neumann |
-- construct a conceptual infinite list of numbers starting at 1 numbersFrom n = n : numbersFrom (n+1) -- conceptual infinite list of square numbers starting at 1 squares = map (^2) (numbersFrom 1) -- take the first 10 square numbers take 10 squares |
Outputs the squares from 1 to
10. |
Squares (4) | Mirko Rahn |
-- ersten zehn Quadrate -- by Mirko Rahn <mai99dla@studserv.uni-leipzig.de> module Main where squares n m = [ i^2 | i <- [n..m] ] main = print $ squares 1 10 -- Ausgabe: [1,4,9,16,25,36,49,64,81,100] |
Outputs the squares from 1 to
10. |
Hello World | Glasgow Haskell Compiler (GHC) | Michael Neumann |
-- file: Main.hs -- compile: ghc --make Main -o main module Main (main) where main = putStr "Hello World\n" |
Prints "Hello World" onto the
screen. |