Wolna encyklopedia
Haskell to funkcyjny język programowania nazwany na cześć Haskella Curry. Jego specyficzne cechy to leniwe wartościowanie, monady, statyczny polimorfizm.
Pliki Haskella mają rozszerzenie .hs
Choć semantyka leniwej ewaluacji może być w wielu zastosowaniach bardzo niewygodna, podobnie jak brak biblioteki porównywalnej z ocamlowską, Haskell nadrabia to wyjątkowo czytelną składnią. W szczególności widać na tym polu kontrast wobec języków takich jak Ocaml.
Kilka przykładów:
-- Komentarz
silnia(1) = 1
silnia(n) = n*silnia(n-1)
silnia2 n = product [1..n]
fib(0) = 0
fib(1) = 1
fib(n+2) = fib(n+1) + fib(n)
ack(0,y) = y+1
ack(x,0) = ack(x-1,1)
ack(x,y) = ack(x-1,ack(x,y-1))
sign x | x>0.0 = 1
sign x | x==0.0 = 0
sign x | x<0.0 = -1
myproduct [] = 1
myproduct [n] = n
myproduct (n:m) = n * myproduct (m)
mysum ([]) = 0
mysum (n:[]) = n
mysum (n:m) = n + mysum (m)
data TreeOfMath =
Mult TreeOfMath TreeOfMath |
Div TreeOfMath TreeOfMath |
Add TreeOfMath TreeOfMath |
Sub TreeOfMath TreeOfMath |
Leaf Float
compute (Mult x y) = compute (x) * compute (y)
compute (Div x y) = compute (x) / compute (y)
compute (Add x y) = compute (x) + compute (y)
compute (Sub x y) = compute (x) - compute (y)
compute (Leaf x) = x
showme (Mult x y) = "(" ++ (showme x) ++ "*" ++ (showme y) ++ ")"
showme (Div x y) = "(" ++ (showme x) ++ "/" ++ (showme y) ++ ")"
showme (Add x y) = "(" ++ (showme x) ++ "+" ++ (showme y) ++ ")"
showme (Sub x y) = "(" ++ (showme x) ++ "-" ++ (showme y) ++ ")"
showme (Leaf x) = show x
qsort [] = []
qsort (x:xs) = qsort less ++ x:(qsort more)
where less = [ a | a <- xs, a < x ]
more = [ a | a <- xs, a >= x ]
-- lista liczb pierwszych
primes = map head $ iterate (\(x:xs)->[y| y<-xs, y `mod` x /= 0]) [2..]
-- lista liczb Fibonacciego
listFib = 1:1:(zipWith (+) listFib (tail listFib))
-- wyrażenia TreeOfMath mają postać: (Sub (Mult (Leaf 5) (Leaf 4)) (Add (Leaf 3) (Leaf 2)))
Linki zewnętrzne
wieloparadygmatowe: Ada • C++ • Common Lisp • D • Fortran • Icon • JavaScript • Nemerle • Perl • Python • Ruby • Snobol
proceduralne i strukturalne: AWK • C • COBOL • Forth • Modula-2 • Oberon • Pascal • PL/SQL • Rey • REXX • sh
obiektowe: C# • Eiffel • Java • Object Pascal • Objective-C • PHP • Smalltalk
funkcyjne: Erlang • F# • Haskell • Lisp • ML • Ocaml • Scheme
inne: ABAP • Asembler • C-- • GAUSS • Lustre • MCPL • SAS 4GL • SQL • Visual Basic • VB.NET • occam • QCL
ezoteryczne: INTERCAL • Brainfuck • BeFunge • Unlambda • Malbolge • Whitespace • FALSE • HQ9+ • Shakespeare • Whirl • Ook
historyczne: ALGOL • APL • BASIC • Clipper • JAS • Lisp • MUMPS • PLAN • PL/I • PL/M • SAKO • SAS (asembler) • Simula