Wie schon $immer hat man nie genug Zeit sich seinen Hobbies zu frönen. Ein Hobby wäre z.B. in der programmiersprache Haskell zu programmieren. Nun bin ich über "Learn You a Haskell" gestossen, was eine recht abgedrehte Haskelleinleitung ist (kann man auch kostenlos online lesen). Ich denke damit sollte es jedem Spaß machen Haskell zu lernen. Zwecks Wiederholung überfliege ich das Buch derzeit und finde dass es hier mal erwähnt werden sollte, nachdem ich per aktuellem Linux-Magazin unter den Buchvorstellungen darauf aufmerksam geworden bin.
Recently in Haskell Category
Um meine Haskellkenntnisse wieder aufzufrischen (in den letzten 3 Monaten habe ich dort relativ wenig gemacht aufgrund anderer Dinge) habe ich mir das Buch Programming in Haskell von Graham Hutton gegönnt.
Es dient Prima als Nachschlagewerk. Wer allerdings keine Lust auf trockenes Lesen hat, der kann sich den darauf basierenden Video-Lectures zuwenden.
fun make_map_fn f1 = fn (x,y) => f1 x :: y
make_map_fn f1 = \x y -> f1 x : y
fun make_filter_fn f1 =
fn (x,y) => if f1 x then x :: y else y
make_filter_fn f1 =
\x y -> if f1 then x : y else y
fun my_map f l = foldr (make_map_fn f) [] l
my_map f l = foldr (make_map_fn f) [] l
fun my_filter f l = foldr (make_filter_fn f) [] l
my_filter f l = foldr (make_filter_fn f) [] l
Die Haskell Syntax ist darauf optimiert so wenig Boilerplate Code wie möglich zu produzieren. Standard ML ist da allerdings auch nicht gerade schlecht (die Funktionen my_map und my_filter haben in Haskell und in SML quasi die selbe Syntax. SML benutzt lediglich noch das Schlüsselwort fun).
Die interaktive SML shell lässt zu Wünschen übrig. So versuch ich vergebens einen Befehl die aktuell geladene Datei erneut einzulesen. In Haskell (ghci) geht das z.B. mit dem Befehl :r (r für reload) wie folgt:
*Main> :r [1 of 1] Compiling Main ( temp.hs, interpreted ) Ok, modules loaded: Main.
Bei SML hingegen muss ich mit Strg+D den interaktiven Prompt verlassen und die Bash-Histoy benutzen um "sml meinesourcecodedatei.sml" erneut einzulesen. Zwar gibt es einen sml-Befehl eine Sourcecodedatei einzulesen, aber der ist zu lang um ihn jedes Mal erneut eingeben zu können (Mein sml hat auch keine History-Funktion).
Standard ML:
datatype ''a multi
= EMPTY
| ELEM of ''a
| UNION of ''a multi * ''a multi
Haskell:
data (Eq a) => Multi a
= Empty
| Elem a
| Union (Multi a) (Multi a)
deriving Show
Standard ML:
fun number (EMPTY) _ = 0
| number (ELEM x) w = if x = w then 1 else 0
| number (UNION (x,y)) w = (number x w) + (number y w)
fun test_number w =
number (UNION (
EMPTY,
UNION (
ELEM 4,
UNION (
ELEM 6,
UNION (
UNION (
ELEM 4,
ELEM 4
),
EMPTY)
)
)
)) w
Haskell:
number Empty _ = 0
number (Elem x) w = if x == w then 1 else 0
test_number w =
number (Union
Empty
(Union
(Elem 4)
(Union
(Elem 6)
(Union
(Union
(Elem 4)
(Elem 4)
)
Empty)
)
)
) w
Standard ML:
fun simplify (UNION (x,y)) =
let fun is_empty (EMPTY) = true
| is_empty _ = false
val x' = simplify x
val y' = simplify y
in if (is_empty x') andalso (is_empty y')
then EMPTY
else if (is_empty x')
then y'
else if (is_empty y')
then x'
else UNION (x', y')
end
| simplify x = x
Haskell:
simplify (Union x y)
| (isEmpty x') && (isEmpty y') = Empty
| isEmpty x' = y'
| isEmpty y' = x'
| otherwise = Union x' y'
where
isEmpty Empty = True
isEmpty _ = False
x' = simplify x
y' = simplify y
simplify x = x
Standard ML:
fun delete_all m w =
let fun delete_all' (ELEM x) = if x = w then EMPTY else ELEM x
| delete_all' (UNION (x,y)) = UNION (delete_all' x, delete_all' y)
| delete_all' x = x
in simplify (delete_all' m)
end
Haskell:
delete_all m w = simplify (delete_all' m)
where
delete_all' (Elem x) = if x == w then Empty else Elem x
delete_all' (Union x y) = Union (delete_all' x) (delete_all' y)
delete_all' x = x
Standard ML:
fun delete_one m w =
let fun delete_one' (UNION (x,y)) =
let val (x', deleted) = delete_one' x
in if deleted
then (UNION (x', y), deleted)
else let val (y', deleted) = delete_one' y
in (UNION (x, y'), deleted)
end
end
| delete_one' (ELEM x) =
if x = w then (EMPTY, true) else (ELEM x, false)
| delete_one' x = (x, false)
val (m', _) = delete_one' m
in simplify m'
end
Haskell:
delete_one m w = do
let (m', _) = delete_one' m
simplify m'
where
delete_one' (Union x y) =
let (x', deleted) = delete_one' x
in if deleted
then (Union x' y, deleted)
else let (y', deleted) = delete_one' y
in (Union x y', deleted)
delete_one' (Elem x) =
if x == w then (Empty, True) else (Elem x, False)
delete_one' x = (x, False)
The first book I've ordered is Programming in Prolog. This book covers everything I should know about Prolog for my next exam.
Originally published in 1981, this was the first textbook on programming in the Prolog language and is still the definitive introductory text on Prolog. Though many Prolog textbooks have been published since, this one has withstood the test of time because of its comprehensiveness, tutorial approach, and emphasis on general programming applications. Prolog has continued to attract a great deal of interest in the computer science community, and has turned out to be the basis for an important new generation of programming languages and systems for Artificial Intelligence. Since the previous edition of Programming in Prolog, the language has been standardised by the International Organization for Standardization (ISO) and this book has been updated accordingly. The authors have also introduced some new material, clarified some explanations, corrected a number of minor errors, and removed appendices about Prolog systems that are now obsolete.
This book was available for just 5 EUR (used)!
The second book I ordered is The Haskell Road to Logic, Maths and Programming:
The purpose of this book is to teach logic and mathematical reasoning in practice, and to connect logical reasoning with computer programming in Haskell. Haskell emerged in the last decade as a standard for lazy functional programming, a programming style where arguments are evaluated only when the value is actually needed. Haskell is a marvellous demonstration tool for logic and maths because its functional character allows implementations to remain very close to the concepts that get implemented, while the laziness permits smooth handling of infinite data structures.
I gonna start to read those books AFTER finishing my previous book which is still very interesting! :)
In order to learn functional programming and to calculate server loads at work I programmed in my spare time this small tool using the purely functional programming language named Haskell! SLoad stands for Serverload. And this is exactly what it is for. SLoad only uses basic parameters (current CPU peak, current requests per second) in order approximate the future need of servers. The website is located at sload.buetow.org.
