Haskell    hoch http://www.haskell.org
  ähnliche Sprachen: ML   Gofer   Hope   Objective Caml   Miranda  
  Beschreibung: Haskell is a pure lazy functional programming language:
  • lazy evaluation: infinite (e.g. "ones = 1:ones" is possible), cyclic datastructures; excution only when needed; non strict functions
  • polymorph: polymorphic functions (e.g. many list operations); overloading (ad hoc polymorhism), e.g. + is defined on several datatypes (other functional languages have for that different operators, e.g. Ocaml has + for Integers and +. for Floats)
  • No semicolons needed: layout driven (but possible to use explicit semicolons and braces)


  Fakultät (1)   Michael Neumann
fac 0 = 1
fac n = n * fac (n-1)


-- type "fac 6" to calculate the result 
Berechnet die Fakultät. Ergibt 720.


  Fakultät (2)   Michael Neumann
fac n
  | n == 0    = 1
  | otherwise = n * fac (n-1)


-- type "fac 6" to calculate the result 
Berechnet die Fakultät. Ergibt 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]
Gibt die ersten 20 Fibonacci-Zahlen aus.


 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"
Gibt "Hello World" auf dem Bildschirm aus.


 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-Sortieralgorithmus


 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])
Gibt die Quadrate von 1 bis 10 aus.


 Squares (2)   Matthias Mann
--
-- von Matthias Mann
--

module Squares (main) where

import List

main =
   putStr . concat . intersperse " " . map (show . (^2))) [1..10]
Gibt die Quadrate von 1 bis 10 aus.


 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
Gibt die Quadrate von 1 bis 10 aus.


 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]
Gibt die Quadrate von 1 bis 10 aus.


 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"
Gibt "Hello World" auf dem Bildschirm aus.