Fedora - Getting Started with Haskell on Fedora - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Computers (https://www.sickgaming.net/forum-86.html) +--- Forum: Linux, FreeBSD, and Unix types (https://www.sickgaming.net/forum-88.html) +--- Thread: Fedora - Getting Started with Haskell on Fedora (/thread-95887.html) |
Fedora - Getting Started with Haskell on Fedora - xSicKxBot - 06-25-2020 Getting Started with Haskell on Fedora <div><div class="wp-block-group"> <div class="wp-block-group__inner-container"> <p><a href="https://www.haskell.org/">Haskell</a> is a functional programming language. To create a program, the user applies and composes functions, in comparison to imperative languages, which use procedural statements.</p> <p>Haskell features state-of-the-art programming paradigms that can be used for general purpose programs, research, and industrial applications. Most notably, it is purely functional using an advanced type system, which means that every computation, even the ones that produce side-effecting IO operations, are defined using pure functions in the mathematical sense.</p> <p> <span id="more-31194"></span> </p> <div class="wp-block-jetpack-markdown"> <p>Some of the main reasons for Haskell’s increasing popularity are ease of maintenance, extensive packages, multi-core performance, and language safety against some bugs, such as null pointers or deadlock. So let’s see how to get started with Haskell on Fedora.</p> <h1>Install Haskell in Fedora</h1> <p>Fedora provides an easy way to install the Glasgow Haskell Compiler (GHC) via the official repository:</p> </div> </div> </div> <pre class="wp-block-preformatted">$ sudo dnf install -y ghc $ ghc --version The Glorious Glasgow Haskell Compilation System, version 8.6.5</pre> <div class="wp-block-jetpack-markdown"> <p>Now that GHC is installed, let’s write a simple program, compile it, and execute it.</p> <h1>First program in Haskell</h1> <p>Let’s write the traditional “Hello, World!” program in Haskell. First, create a <em>main.hs</em> file, and then type or copy the following.</p> </div> <pre class="wp-block-preformatted">main = putStrLn ("Hello, World!")</pre> <div class="wp-block-jetpack-markdown"> <p>Running this program is quite simple.</p> </div> <pre class="wp-block-preformatted">$ runhaskell main.hs Hello, World!</pre> <div class="wp-block-jetpack-markdown"> <p>Building an executable of the program is as simple as running it.</p> </div> <pre class="wp-block-preformatted">$ ghc main.hs [1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... $ ./main Hello, World!</pre> <div class="wp-block-jetpack-markdown"> <p>GHC provides a Read Eval Print Loop (REPL) command to interactively evaluate Haskell expressions.</p> </div> <pre class="wp-block-preformatted">$ ghci Prelude> :load main.hs [1 of 1] Compiling Main ( main.hs, interpreted ) Ok, one module loaded. *Main> main Hello, World! *Main> :type putStrLn putStrLn :: String -> IO () *Main> :info String type String = [Char] -- Defined in ‘GHC.Base’ *Main> :help ...</pre> <div class="wp-block-jetpack-markdown"> <p>The most common REPL commands are:</p> <ul> <li><em>:load</em> loads a file.</li> <li><em>:reload</em> reloads loaded files.</li> <li><em>:type</em> shows the type of an expression.</li> <li><em>:info</em> displays information about a name.</li> <li><em>:browse</em> lists the loaded functions.</li> </ul> <h1>Using Haskell packages</h1> <p>Haskell features a package system to share functions.</p> <p>To show how to use packages, let’s write a new one.<br /> First, create a <em>MyPackage.hs</em> file, and then, type or copy the following.</p> </div> <pre class="wp-block-preformatted">module MyPackage where greet name = putStrLn ("Hello, " ++ name ++ "!")</pre> <div class="wp-block-jetpack-markdown"> <p>Next, modify the <em>main.hs</em> file as follow:</p> </div> <pre class="wp-block-preformatted">import MyPackage main = greet ("Fedora")</pre> <div class="wp-block-jetpack-markdown"> <p>In the modified <em>main.hs</em>, instead of using the standard <em>putStrLn</em> function to print the “Hello, World!”<br /> the application now uses the greet function from the newly created package:</p> </div> <pre class="wp-block-preformatted">$ runhaskell main.hs Hello, Fedora!</pre> <div class="wp-block-jetpack-markdown"> <p>GHC automatically looks for packages in the current working directory, and it<br /> can also looks for packages installed globally on the system. To use a Fedora package in your Haskell project,<br /> you need to install the development files. For example, let’s use the <em>ansi-wl-pprint</em> library to add color:</p> </div> <pre class="wp-block-preformatted">$ sudo dnf install -y ghc-ansi-wl-pprint-devel</pre> <div class="wp-block-jetpack-markdown"> <p>Next, modify the <em>MyPackage.hs</em> file:</p> </div> <pre class="wp-block-preformatted">module MyPackage where import Text.PrettyPrint.ANSI.Leijen greet name = putDoc (green (text ("Hello, " ++ name ++ "!\n")))</pre> <div class="wp-block-jetpack-markdown"> <p>Then you can build a small executable using dynamic links:</p> </div> <pre class="wp-block-preformatted">$ ghc -dynamic main.hs -o greet && strip greet [1 of 2] Compiling MyPackage ( MyPackage.hs, MyPackage.o ) [2 of 2] Compiling Main ( main.hs, main.o ) Linking greet ... $ du --si greet 17k greet $ ./greet <span style="color:#4b6f44" class="has-inline-color">Hello, Fedora!</span></pre> <div class="wp-block-jetpack-markdown"> <p>This concludes how to get started with Haskell on Fedora. You can find the Haskell SIG (<a href="https://fedoraproject.org/wiki/Category:SIGs">Special Interests Groups</a>) on Freenode IRC in #fedora-haskell, or in the <a href="https://lists.fedoraproject.org/mailman/listinfo/haskell">mailing list</a></p> <h1>Learning resources</h1> <p>Haskell may be daunting because it supports many advanced concepts such as <em>GADTs</em> or <em>type-level programing</em>.<br /> However, the basics are quite accessible and they are more than enough to reap the majority of benefits and reliably deliver quality software.</p> <p>To learn more about the language, here are some further resources:</p> <ul> <li><a href="https://github.com/thma/WhyHaskellMatters">Why Haskell Matter</a></li> <li><a href="http://dev.stephendiehl.com/hask/">What I Wish I Knew When Learning Haskell</a></li> <li><a href="https://hackage.haskell.org/package/turtle-1.5.18/docs/Turtle-Tutorial.html">Shell programming, Haskell-style</a></li> <li><a href="https://www.seas.upenn.edu/~cis194/spring13/lectures.html">Yorgey’s cis194 course</a></li> <li><a href="https://lorepub.com/product/haskellbook">Haskell Programming from First Principles</a></li> </ul> </div> </div> https://www.sickgaming.net/blog/2020/06/24/getting-started-with-haskell-on-fedora/ |