Haskell - przykładowe programy

Program obliczający pierwiastki równania kwadratowego

(1)
\begin{verbatim} data Pierwiastki = Brak | Jeden Double | Dwa (Double, Double) deriving Show pierwiastki :: (Double, Double, Double) -> Pierwiastki pierwiastki(0, b, c) b==0 = Brak otherwise = Jeden (-c/b) pierwiastki(a, b, c) det<0 = Brak det==0 = Jeden(-b/(2*a)) det>0 = Dwa((-b-sqrt det)/(2*a),(-b+sqrt det)/(2*a)) where det = b^2=4*a*c \end{verbatim}

Sortowanie metodą QuickSort

(2)
\begin{verbatim} qsort :: Ord a => [a] -> [a] qsort [] = [] qsort (x:xs) = [y| y<-xs, y<=x] ++[x] ++qsort[y| y<-xs, y>x] \end{verbatim}

Sortowanie metodą InsertionSort

(3)
\begin{verbatim} insertionSort :: Ord a => [a] -> [a] insertionSort [] = [] insertionSort (x:xs) = insert.insertionSort $ xs where insert [] = [x] insert ys@(y:ys') x >= y = y : insert ys' otherwise = x:ys \end{verbatim}

Sortowanie metodą SelectionSort

(4)
\begin{verbatim} selectionSort :: Ord a => [a] -> [a] selectionSort [] = [] selectionSort xs = y : selectionSort ys where (y:ys) = select xs select ys@[_] = ys select (y:ys) y<z = y:zs otherwise = z:y:zs' where zs@(z:zs') = select ys \end{verbatim}
wersja strony: 8, ostatnia edycja: 1214074020|%e %b %Y, %H:%M %Z (%O temu)
Jeśli nie zaznaczono inaczej, Zawartość tej strony dostępna jest na licencji Creative Commons Attribution-ShareAlike 3.0 License