Haskell Language

Total Page:16

File Type:pdf, Size:1020Kb

Haskell Language Haskell Language #haskell 1 1: Haskell 2 2 2 2 Examples 2 2 3 4 1 4 2 4 Fibonacci 4 5 REPL 5 GHCI 5 7 7 100 7 7 7 7 7 7 7 2: Attoparsec 9 9 9 Examples 9 9 - 10 3: Bifunctor 11 11 11 Examples 11 Bifunctor 11 11 Either 11 11 Bifunctor 12 4: Data.Aeson - HaskellJSON 13 Examples 13 13 Data.Aeson.Value 13 14 5: Data.Text 15 15 Examples 15 15 15 16 16 TextText 16 17 6: GHCJS 18 18 Examples 18 “Hello World”Node.js 18 7: Google Protocol Buffers 19 19 Examples 19 .proto 19 8: Gtk3 21 21 21 Examples 21 GtkHello World 21 9: IO 22 Examples 22 22 22 22 22 23 24 IO“main” 24 IO 25 IO 25 IO 26 IO 26 IOdo 26 'a'“”'IO a' 27 stdout 27 putChar :: Char -> IO () - charstdout 27 putStr :: String -> IO () - Stringstdout 27 putStrLn :: String -> IO () - Stringstdout 27 print :: Show a => a -> IO () - a Showstdout 27 `stdin` 28 getChar :: IO Char - stdinChar 28 getLine :: IO String - stdinString 28 read :: Read a => String -> a - String 28 10: Monad 30 Examples 30 monadic 30 30 31 32 11: Profunctor 33 33 33 33 Examples 33 - >Profunctor 33 12: Web 34 Examples 34 34 35 13: XML 36 36 Examples 36 `xml` 36 14: zipWithM 37 37 37 Examples 37 37 15: 38 38 Examples 38 `forkIO` 38 “MVar” 38 39 atomically :: STM a -> IO a 39 readTVar :: TVar a -> STM a 39 writeTVar :: TVar a -> a -> STM () 39 16: 41 41 Examples 41 41 41 41 41 41 42 42 17: 43 Examples 43 Monoid 43 Monoids 43 43 Monoid 44 18: 45 Examples 45 45 “” 45 45 19: RankNTypes 47 47 47 Examples 47 RankNTypes 47 20: GHCi 48 48 Examples 48 GHCi 48 GHCi 48 GHCi 48 48 GHCi 49 49 GHCi 49 50 21: taggy-lensHTML 51 Examples 51 iddiv 51 51 22: 53 Examples 53 53 53 23: ...... 54 54 Examples 54 54 54 55 55 uncurrying 55 currying 56 56 56 24: Monads 57 Examples 57 monadmonadic 57 Monads 58 foldFreeiterM 58 monad 59 25: 61 61 61 61 61 Examples 61 Functor 61 61 62 62 63 Functor 63 63 63 64 Functor 64 Functor 64 Functor 65 65 65 66 26: 67 Examples 67 67 67 68 27: 69 69 69 Examples 69 69 69 - 1 69 - 2 70 28: 71 Examples 71 71 71 71 71 Haskell 72 72 72 72 73 Hask 73 73 73 74 74 Hask 74 74 74 Hask 74 Haskell 75 29: 76 Examples 76 76 76 76 77 77 77 77 30: 78 Examples 78 78 78 78 78 78 78 78 31: 80 80 Examples 80 80 81 81 32: 82 Examples 82 82 82 83 EventNetworks 83 33: 85 85 85 Examples 85 85 85 85 86 87 Monoid 88 88 88 34: 90 90 Examples 90 monad 90 IO monad 92 Monad 92 Monad 93 monadic 93 94 Monad 94 35: 96 Examples 96 96 96 96 96 96 97 36: 99 99 99 99 Examples 99 99 100 100 100 37: Monad 101 101 101 Examples 101 101 101 102 postIncrement 102 102 Traversable 102 Traversable 102 38: 104 Examples 104 104 104 104 104 104 StackageLTS 104 104 105 105 105 105 39: 107 107 107 Examples 107 HaskellC 107 HaskellC 108 40: - Data.Map 109 Examples 109 109 109 109 109 110 110 Monoid 110 41: 111 Examples 111 111 111 42: GHC 113 113 Examples 113 MultiParamTypeClasses 113 FlexibleInstances 113 OverloadedStrings 113 TupleSections 114 N 114 114 UnicodeSyntax 114 BinaryLiterals 115 ExistentialQuantification 115 LambdaCase 116 RankNTypes 117 OverloadedLists 117 FunctionalDependencies 118 GADTs 118 ScopedTypeVariables 119 PatternSynonyms 119 RecordWildCards 120 43: monadmonad 121 Examples 121 ~~ 121 ~~Nat~~Nat 121 ~~ MaybeTNat 121 w~~[W] 122 Const c~~ c 122 x~~x 122 44: cofree comonads 124 Examples 124 Cofree~~ 124 CofreeConst c~~c 124 Cofree~~ 124 Cofree~~ NonEmpty 124 Cofreew~~ WriterT w Stream 124 CofreeE e~~ NonEmptyTe 125 Cofreex~~ Moore x 125 45: 126 Examples 126 126 46: 127 Examples 127 127 47: 128 Examples 128 128 128 128 129 ==> 129 129 48: 130 130 130 130 Examples 130 130 130 130 130 131 131 49: 133 133 Examples 133 133 133 133 133 50: 135 Examples 135 135 135 135 136 136 136 51: 137 137 137 Examples 137 Eval Monad 137 RPAR 137 RSEQ 138 52: 140 Examples 140 Postgres 140 140 140 53: 141 141 141 Examples 141 141 141 54: 143 143 143 Examples 143 143 143 144 144 144 144 55: HaskellQuasiQuotes 146 146 Haskell 146 146 Haskell 146 Examples 146 Q 146 147 HaskellQuasiquotes 148 148 QuasiQuotation 148 149 QuasiQuotes 149 149 56: IO 150 Examples 150 IO 150 57: 151 151 151 Examples 152 152 152 152 152 153 153 154 foldl 154 foldr 155 `map` 155 `' 155 156 58: 157 Examples 157 SmallCheckQuickCheckHUnit 157 59: 158 158 Examples 158 Data.Vector 158 158 `map`Reducing`fold` 158 159 60: 160 160 Examples 160 FunctorFoldable 160 Traversable 160 162 162 162 163 164 61: 166 166 166 166 Examples 167 167 `Fractional Int...` 168 168 62: 169 169 Examples 169 169 169 169 runEffect 170 170 monad 170 170 63: 173 Examples 173 173 64: 174 174 174 Examples 174 174 174 174 65: 175 175 Examples 175 hslogger 175 66: 176 Examples 176 176 176 newtype 177 RecordWildCards 177 177 178 NamedFieldPuns 178 RecordWildcards 178 178 67: / 179 179 Examples 179 179 68: 180 180 Examples 180 180 180 181 181 69: 182 182 Examples 182 182 182 183 183 183 183 70: 184 184 Examples 184 184 184 185 185 71: GHC 186 Examples 186 186 72: 187 187 187 187 187 187 Examples 187 187 187 187 188 188 188 188 188 188 189 73: 190 Examples 190 190 190 190 190 191 191 191 191 74: 192 192 192 Examples 192 Functor Class 192 Ord 193 193 193 193 193 193 194 194 194 194 194 194 194 194 194 75: 196 196 196 196 196 196 196 196 Examples 197 197 197 197 197 197 & 197 198 Haskell 198 199 199 200 200 makeFields 200 76: 202 202 Examples 203 203 203 77: 204 Examples 204 204 204 204 204 204 205 205 205 206 206 206 78: 207 207 Examples 207 207 Lambda 207 208 209 You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: haskell-language It is an unofficial and free Haskell Language ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official Haskell Language. The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners. Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected] https://riptutorial.com/zh-TW/home 1 1: Haskell Haskell。 • Haskell。。。 • Haskell。。 • ConcurrentGHC。 • 。。 • Haskell。 • Haskell。 HaskellHaskell 2010.20165Haskell 2020。 Haskell。。 Haskell 2010 2012-07-10 98 2002-12-01 Examples “” Haskell main :: IO () main = putStrLn "Hello, World!" mainIO ()“”type ()I / O“unit”;。 main。 helloworld.hsHaskellGHC ghc helloworld.hs "Hello, World!" ./helloworld Hello, World! runhaskellrunghc runhaskell helloworld.hs https://riptutorial.com/zh-TW/home 2 REPL。HaskellGHCghci ghci> putStrLn "Hello World!" Hello, World! ghci> load :l ghci ghci> :load helloworld :reload :r ghci Prelude> :l helloworld.hs [1 of 1] Compiling Main ( helloworld.hs, interpreted ) <some time later after some edits> *Main> :r Ok, modules loaded: Main. main main :: IO () IO ()。 HaskellHindley-Milnermain :: IO () 。main 。。 • Haskell。GHCi“”。 • bug 。。。。 main = putStrLn "Hello, World!" main = do { putStrLn "Hello, World!" ; return () } Haskell; main = do putStrLn "Hello, World!" return () domonadic I / O domonadI / O。 domonadIO returnmonad。 main = putStrLn "Hello, World!" https://riptutorial.com/zh-TW/home 3 putStrLn "Hello, World!"IO () 。“” putStrLn "Hello, World!"main。 putStrLn putStrLn :: String -> IO () -- thus, putStrLn (v :: String) :: IO () putStrLnI / O。mainputStrLn "Hello, World!" 。 Haskell“Hello World” 。 1 fac :: (Integral a) => a -> a fac n = product [1..n] • Integral。IntInteger 。 • (Integral a) =>a • fac :: a -> a facaa • product。 • [1..n]desugarsenumFromTo 1 n 1 ≤ x ≤ n 。 2 fac :: (Integral a) => a -> a fac 0 = 1 fac n = n * fac (n - 1) 。0 。fac。 GHCfac。。 Fibonacci Haskell。 f (0) <- 0 f (1) <- 1 f (n) <- f (n-1) + f (n-2) 。 fibs !! n <- f (n) ┌──────┐ ┌──────┐ ┌──────┐ https://riptutorial.com/zh-TW/home 4 │ f(0) │ │ f(1) │ │ f(2) │ fibs -> 0 : 1 : │ + │ : │ + │ : │ + │ : ..... │ f(1) │ │ f(2) │ │ f(3) │ └──────┘ └──────┘ └──────┘ ┌────────────────────────────────────────┐ │ f(0) : f(1) : f(2) : ..... │ └────────────────────────────────────────┘ -> 0 : 1 : + ┌────────────────────────────────────────┐ │ f(1) : f(2) : f(3) : ..... │ └────────────────────────────────────────┘ fibn n = fibs !! n where fibs = 0 : 1 : map f [2..] f n = fibs !! (n-1) + fibs !! (n-2) GHCi> let fibs = 0 : 1 : zipWith (+) fibs (tail fibs) GHCi> take 10 fibs [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] zipWithzipWith (+) [x1, x2, ...] [y1, y2, ...][x1 + y1, x2 + y2, ...] 。 fibsscanl GHCi> let fibs = 0 : scanl (+) 1 fibs GHCi> take 10 fibs [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] scanlfoldl。 scanl f z0 [x1, x2, ...][z0, z1, z2, ...] where z1 = f z0 x1; z2 = f z1 x2; ... 。fibFibonaccin GHCi> let fib n = fibs !! n -- (!!) being the list subscript operator -- or in point-free style: GHCi> let fib = (fibs !!) GHCi> fib 9 34 REPL HaskellHaskellHaskellREPLread-eval-print-loop。REPLIO。help。Haskell。 GHCI GHCiGlorious / Glasgow Haskell。 GHC。CabalStack。UnixStack curl -sSL https://get.haskellstack.org/ | sh https://riptutorial.com/zh-TW/home 5 GHC。stack。Haskell 。 1.
Recommended publications
  • State of the Platform Haskell Implementors Workshop - 2012 Stats
    Mark Lentczner, Google State of the Platform Haskell Implementors Workshop - 2012 Stats Number of packages — 47 (21 ghc + 26 hp) Lines of Code — 302k (167k ghc + 135k hp) Releases — 9 (May 2009 ~ present) Distributions — 11+ (Mac, Windows, Linuxes) Stats — June ~ August 2012 Downloads — 20,957 (228/day) 68% Windows 21% Mac OS X 8% Source ?? Linux Mentions of "platform" on #haskell — 983 People Build Maintainers Committee Joachim Breitner — Debian Duncan Coutts Mikhail Glushenkov — Windows Iavor Diatchki Mark Lentczner — OS X Isaac Dupree Andres Löh — NixOS Thomas Schilling Gabor Pali — FreeBSD Johan Tibell Jens Petersen — Fedora Adam Wick Release Team And the many contributors on Mark Lentczner — Chief Meanie haskell-platform@ Duncan Coutts libraries@ Don Stewart Content Packages — Haskell Platform 2012.2.0.0 ghc 7.4.1 time 1.4 random 1.0.1.1 array 0.4.0.0 unix 2.5.1.0 regex-base 0.93.2 base 4.5.0.0 Win32 2.2.2.0 regex-compat 0.95.1 bytestring 0.9.2.1 regex-posix 0.95.1 Cabal 1.14.0 stm 2.3 containers 0.4.2.1 syb 0.3.6.1 deepseq 1.3.0.0 cgi 3001.1.7.4 text 0.11.2.0 directory 1.1.0.2 fgl 5.4.2.4 transformers 0.3.0.0 extensible-exceptions GLUT 2.1.2.1 xhtml 3000.2.1 0.1.1.4 haskell-src 1.0.1.5 zlib 0.5.3.3 filepath 1.3.0.0 html 1.0.1.2 haskell2010 1.1.0.1 HTTP 4000.2.3 haskell98 2.0.0.1 HUnit 1.2.4.2 hpc 0.5.1.1 mtl 2.1.1 cabal-install 0.14.0 old-locale 1.0.0.4 network 2.3.0.13 alex 3.0.1 old-time 1.1.0.0 OpenGL 2.2.3.1 happy 1.18.9 pretty 1.1.1.0 parallel 3.2.0.2 process 1.1.0.1 parsec 3.1.2 template-haskell 2.7.0.0 QuickCheck 2.4.2 Standard
    [Show full text]
  • Notes on Functional Programming with Haskell
    Notes on Functional Programming with Haskell H. Conrad Cunningham [email protected] Multiparadigm Software Architecture Group Department of Computer and Information Science University of Mississippi 201 Weir Hall University, Mississippi 38677 USA Fall Semester 2014 Copyright c 1994, 1995, 1997, 2003, 2007, 2010, 2014 by H. Conrad Cunningham Permission to copy and use this document for educational or research purposes of a non-commercial nature is hereby granted provided that this copyright notice is retained on all copies. All other rights are reserved by the author. H. Conrad Cunningham, D.Sc. Professor and Chair Department of Computer and Information Science University of Mississippi 201 Weir Hall University, Mississippi 38677 USA [email protected] PREFACE TO 1995 EDITION I wrote this set of lecture notes for use in the course Functional Programming (CSCI 555) that I teach in the Department of Computer and Information Science at the Uni- versity of Mississippi. The course is open to advanced undergraduates and beginning graduate students. The first version of these notes were written as a part of my preparation for the fall semester 1993 offering of the course. This version reflects some restructuring and revision done for the fall 1994 offering of the course|or after completion of the class. For these classes, I used the following resources: Textbook { Richard Bird and Philip Wadler. Introduction to Functional Program- ming, Prentice Hall International, 1988 [2]. These notes more or less cover the material from chapters 1 through 6 plus selected material from chapters 7 through 9. Software { Gofer interpreter version 2.30 (2.28 in 1993) written by Mark P.
    [Show full text]
  • Graphical User Interfaces in Haskell
    Graphical User Interfaces in Haskell By Gideon Sireling August 2011 Abstract Graphical user interfaces (GUIs) are critical to user friendliness, and are well supported by imperative, particularly object-oriented, programming languages. This report focuses on the development of GUIs with the purely functional language Haskell. We review prior efforts at supporting such interfaces with functional idioms, and investigate why these are rarely used in practice. We argue that there is no overarching solution, but rather, that each class of graphical application should be supported by a domain-specific abstraction. Finally, we present such an abstraction for data-processing applications; a framework for binding data to graphical interfaces. The framework does not attempt to replace existing graphical toolkits; rather, it adds a new layer of abstraction, with interfaces provided to Gtk2Hs and WxHaskell. Simple examples demonstrate how Haskell can be utilised to accomplish this task as easily as any imperative language. 1 Acknowledgement I would like to thank my supervisor, Mr Andy Symons, for the clear direction which he provided throughout this project. I would also like to thank the good folks who so freely donate their time and expertise to the Haskell Platform, EclipseFP, and all the community projects utilised in this work. Finally, I wish to express my deepest gratitude to my family, who tolerated all the time invested in this project with the utmost grace and patience. 2 Table of Contents Abstract ..............................................................................................................................................
    [Show full text]
  • Haskell Und Debian
    Haskell und Debian Joachim Breitner ∗ Karlsruher Institut für Technology [email protected] Abstract gleiche Weise direkt installiert werden: Ein einfaches apt-get install ghc Da die Programmiersprache Haskell immer beliebter wird genügt, und der Haskell-Compiler ist installiert. apt-get upgrade und in immer weiteren Kreisen eingesetzt wird, müssen sich Aktualisierungen laufen problemlos mit apt-get remove nun immer mehr Programmierer auch um die Besonderheiten und nicht benötigte Pakete können mit sau- der Installation von Haskell-Compilern und -Bibliotheken ber und vollständig entfernt werden. Wenn ein zu installie- kümmern. Dies zu vereinfachen und vereinheitlichen ist die rendes Paket ein anderes benötigt, wird dieses automatisch originäre Aufgabe von Distributionen wie Debian. Dieser mit installiert und auch im weiteren Betrieb werden diese Vortrag gibt einen Einblick in die Funktionsweise von Debian, Abhängigkeiten stets geprüft. Kryptographie schützt den An- insbesondere in Bezug auf Haskell-Pakete, erklärt was man wender dabei vor bösartig veränderten Paketen. als Anwender von Debian erwarten kann und was sich Darüber hinaus prüfen die Debian-Entwickler dass die Debian von den Haskell-Bibliotheken wünscht. Programme auch wirklich Freie Software sind. Man kann sich also darauf verlassen, dass man zu jedem Paket in Debian die Categories and Subject Descriptors K.6.3 [Management of Quellen bekommt, diese verändern darf und die Veränderun- Computing and Information Systems]: Software Management— gen auch weitergeben darf. Auch das geht mit einheitlichen Software process Befehlen (apt-get source und dpkg-buildpackage). Keywords linux distribution, package management, Debian Ich erinnere mich noch vage an die Zeit als ich unter Windows Software noch auf irgendwelchen Webseiten direkt heruntergeladen habe und mich durch immer verschiedene 1.
    [Show full text]
  • Haskell Communities and Activities Report
    Haskell Communities and Activities Report http://tinyurl.com/haskcar Twenty-Second Edition — May 2012 Janis Voigtländer (ed.) Andreas Abel Iain Alexander Heinrich Apfelmus Emil Axelsson Christiaan Baaij Doug Beardsley Jean-Philippe Bernardy Mario Blažević Gwern Branwen Joachim Breitner Björn Buckwalter Douglas Burke Carlos Camarão Erik de Castro Lopo Roman Cheplyaka Olaf Chitil Duncan Coutts Jason Dagit Nils Anders Danielsson Romain Demeyer James Deng Dominique Devriese Daniel Díaz Atze Dijkstra Facundo Dominguez Adam Drake Andy Georges Patai Gergely Jürgen Giesl Brett G. Giles Andy Gill George Giorgidze Torsten Grust Jurriaan Hage Bastiaan Heeren PÁLI Gábor János Guillaume Hoffmann Csaba Hruska Oleg Kiselyov Michal Konečný Eric Kow Ben Lippmeier Andres Löh Hans-Wolfgang Loidl Rita Loogen Ian Lynagh Christian Maeder José Pedro Magalhães Ketil Malde Antonio Mamani Alp Mestanogullari Simon Michael Arie Middelkoop Dino Morelli JP Moresmau Ben Moseley Takayuki Muranushi Jürgen Nicklisch-Franken Rishiyur Nikhil David M. Peixotto Jens Petersen Simon Peyton Jones Dan Popa David Sabel Uwe Schmidt Martijn Schrage Tom Schrijvers Andrew G. Seniuk Jeremy Shaw Christian Höner zu Siederdissen Michael Snoyman Jan Stolarek Martin Sulzmann Doaitse Swierstra Henning Thielemann Simon Thompson Sergei Trofimovich Marcos Viera Janis Voigtländer David Waern Daniel Wagner Greg Weber Kazu Yamamoto Edward Z. Yang Brent Yorgey Preface This is the 22nd edition of the Haskell Communities and Activities Report. As usual, fresh entries are formatted using a blue background, while updated entries have a header with a blue background. Entries for which I received a liveness ping, but which have seen no essential update for a while, have been replaced with online pointers to previous versions.
    [Show full text]
  • Preview Haskell Tutorial (PDF Version)
    Haskell Programming About the Tutorial Haskell is a widely used purely functional language. Functional programming is based on mathematical functions. Besides Haskell, some of the other popular languages that follow Functional Programming paradigm include: Lisp, Python, Erlang, Racket, F#, Clojure, etc. Haskell is more intelligent than other popular programming languages such as Java, C, C++, PHP, etc. In this tutorial, we will discuss the fundamental concepts and functionalities of Haskell using relevant examples for easy understanding. Audience This tutorial has been prepared for beginners to let them understand the basic concepts of functional programming using Haskell as a programming language. Prerequisites Although it is a beginners’ tutorial, we assume that the readers have a reasonable exposure to any programming environment and knowledge of basic concepts such as variables, commands, syntax, etc. Copyright & Disclaimer © Copyright 2017 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected] i Haskell Programming Table of Contents About the Tutorial ......................................................................................................................................
    [Show full text]
  • TU Kaiserslautern Functional Programming
    Prof. Dr. Ralf Hinze TU Kaiserslautern M.Sc. Sebastian Schweizer Fachbereich Informatik AG Programmiersprachen Functional Programming: Exercise 1 Sheet published: Wednesday, April 17th Submission deadline: Tuesday, April 23rd, 12:00 noon Registration deadline: Thursday, April 18th, 12:00 noon Submission Instructions After you have registered for the exercise (see task 1), you will get access to your team's Git repository on our GitLab server. Please do the submission for tasks 3 and 4 into that repository by Tuesday, April 23rd, 12:00 noon. 1 Register for Exercises In order to take this course you need to register for the exercises. Please form teams of 3{4 students each. Send an email including your name, contact email address, matrikulation number, as well as the names of your teammates (names only) to Sebastian Schweizer ([email protected]). The registration deadline is Thursday, April 18th, 12:00 noon. If you cannot find colleagues for a team then we will arrange something during the first exercise session. We will register an account for you on our GitLab server and create a repository for your team. You have to submit your exercise solutions for this and the following sheets into that repository. We will provide the following exercise sheets directly into your repository. Furthermore, you will get read access to an additional repository containing the lecture slides. 2 Tool Setup In this course, we use the standard lazy functional programming language Haskell. In order to solve the exercises, you need a Haskell compiler. Furthermore, you need Git to submit your solutions into your team's repository and to obtain the lecture material.
    [Show full text]
  • Why Learn Haskell?
    Why learn Haskell? Keegan McAllister SIPB Cluedump October 11, 2011 Keegan McAllister Why learn Haskell? Composability The central challenge of programming (Dijkstra, 2000): How not to make a mess of it It helps to build programs from composable parts Combine in flexible yet well-defined ways Haskell is a language uniquely suited to this goal Keegan McAllister Why learn Haskell? Functions factorial 0=1 factorial n= n * factorial (n-1) Whitespace for function application f x not f(x) Parentheses only for grouping Keegan McAllister Why learn Haskell? Lists A list is either the empty list [], or a first element x and a remaining list xs, written (x:xs) Use these patterns to build and to inspect lists length []=0 length (x:xs)= 1 + length xs Keegan McAllister Why learn Haskell? Declarative programming Describe results, not individual steps -- merge two sorted lists merge xs []= xs merge [] ys= ys merge (x:xs) (y:ys) |x<y= x : merge xs (y:ys) | otherwise= y : merge (x:xs) ys Keegan McAllister Why learn Haskell? Equational reasoning Functions on functions map f []=[] map f (x:xs)= f x : map f xs (f . g) x= f (g x) Reason by substituting equals for equals map f (map g xs) ≡ map (f . g) xs map f . map g ≡ map (f . g) Keegan McAllister Why learn Haskell? Lazy evaluation Expressions aren't evaluated until result is needed -- two infinite lists evens= 0 : map (+1) odds odds= map (+1) evens GHCi> take 16 evens [0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30] Keegan McAllister Why learn Haskell? Laziness separates concerns: example 1 minimum= head .
    [Show full text]
  • Haskell High Performance Programming Table of Contents
    Haskell High Performance Programming Table of Contents Haskell High Performance Programming Credits About the Author About the Reviewer www.PacktPub.com eBooks, discount offers, and more Why subscribe? Preface What this book covers What you need for this book Who this book is for Conventions Reader feedback Customer support Downloading the example code Downloading the color images of this book Errata Piracy Questions 1. Identifying Bottlenecks Meeting lazy evaluation Writing sum correctly Weak head normal form Folding correctly Memoization and CAFs Constant applicative form Recursion and accumulators The worker/wrapper idiom Guarded recursion Accumulator parameters Inspecting time and space usage Increasing sharing and minimizing allocation Compiler code optimizations Inlining and stream fusion Polymorphism performance Partial functions Summary 2. Choosing the Correct Data Structures Annotating strictness and unpacking datatype fields Unbox with UNPACK Using anonymous tuples Performance of GADTs and branching Handling numerical data Handling binary and textual data Representing bit arrays Handling bytes and blobs of bytes Working with characters and strings Using the text library Builders for iterative construction Builders for strings Handling sequential data Using difference lists Difference list performance Difference list with the Writer monad Using zippers Accessing both ends fast with Seq Handling tabular data Using the vector package Handling sparse data Using the containers package Using the unordered-containers package Ephemeral
    [Show full text]
  • Easing Software Component Repository Evolution ∗
    Easing Software Component Repository Evolution ∗ Jérôme Vouillon Mehdi Dogguy Roberto Di Cosmo CNRS, PPS UMR 7126, EDF S.A., France, Univ Paris Diderot, Univ Paris Diderot, Debian Release Team, Sorbonne Paris Cité, Sorbonne Paris Cité, France Debian Project PPS, UMR 7126 CNRS, [email protected] [email protected] INRIA, France paris-diderot.fr [email protected] ABSTRACT source software (FOSS). These components are usually made Modern software systems are built by composing compo- available via a repository, which are storage locations from nents drawn from large repositories, whose size and com- which they can be retrieved. A large variety of repositories plexity increase at a fast pace. Maintaining and evolving are available, ranging from specialised ones for components these software collections is a complex task, and a strict written in a given programming language, like CPAN, Hack- qualification process needs to be enforced. We studied in age or PyPI, application-specific ones, like the Eclipse Plu- depth the Debian software repository, one of the largest and gin collection [13], and more general repositories like Maven most complex existing ones, and we developed comigrate, Central [6] or most GNU/Linux distributions. All these an extremely efficient tool that is able to identify the largest component repositories share the common concern of organ- sets of components that can migrate to the reference reposi- ising the process of integrating changes: new components tory without violating its quality constraints. This tool out- are regularly added (Debian grew by more than 8000 pack- performs significantly existing tools, and provides detailed ages since the last stable release two years ago), outdated information that is crucial to understand the reasons why versions are being replaced by more recent ones, and super- some components cannot migrate.
    [Show full text]
  • Multicore Haskell Now!
    Multicore Haskell Now! Don Stewart | Open Source Bridge | June 2010 © 2009 Galois, Inc. All rights reserved. The Grand Challenge • Making effective use of multicore hardware is the challenge for programming languages now • Hardware is getting increasingly complicated: – Nested memory hierarchies – Hybrid processors: GPU + CPU, Cell, FPGA... – Massive compute power sitting mostly idle • Need new programming models to program commodity machines effectively © 2009 Galois, Inc. All rights reserved. Haskell is ... • A purely functional language • Strongly statically typed • 20 years old http://haskell.org • Born open source http://haskell.org/platform http://hackage.haskell.org • Compiled and interpreted • Used in research, open source and industry • Built for parallel programming © 2009 Galois, Inc. All rights reserved. We got libraries now © 2009 Galois, Inc. All rights reserved. More concerning... © 2009 Galois, Inc. All rights reserved. Haskell and Parallelism: Why? • Language reasons: – Purity, laziness and types mean you can find more parallelism in your code – No specified execution order – Speculation and parallelism safe. • Purity provides inherently more parallelism • High level: shorter code. © 2009 Galois, Inc. All rights reserved. Haskell and Parallelism • Statically typed and heavily optimized: more performance. • Custom multicore runtime: thread performance a primary concern – thanks Simon Marlow! • Mature: 20 year code base, long term industrial use, big library system • Demonstrated performance – emphasis on shared memory systems © 2009 Galois, Inc. All rights reserved. © 2009 Galois, Inc. All rights reserved. The Goal • Parallelism: exploit parallel computing hardware to improve performance • Concurrency: logically independent tasks as a structuring technique • Improve performance of programs by using multiple cores at the same time • Improve performance by hiding latency for IO- heavy programs © 2009 Galois, Inc.
    [Show full text]
  • 1 the Standard Haskell Infrastructure [Infrastructure.Tex 2011-11-17]
    infrastructure.tex 2011-11-17 haskell doc September 6, 2012 1 1 The standard Haskell infrastructure [infrastructure.tex 2011-11-17] Announcement Forword I shouldn’t say, that the Haskell Cabal is a mess. Maybe, it is a Haskell amateurs write Haskell modules, Haskell professionals good design and the cabal command is already a powerful and write packages. versatile tool. But its documentation is still a mess: all the good Modules are part of the Haskell language in the narrower sense Haskell books out there either don’t deal with packages at all, of a well–defined syntax and pretty well–explained semantics. or they sketch typical examples without explaining a lot. And Packages are defined by a standard beyond, namely the Ca- the Cabal User Guide itself is rather an overwhelming reference bal, the Common Architecture for Building Applications and Li- than a tutorial. braries. I should understand the matter to write this text. But I wrote In the sequel, we try to shed some light on the standard Haskell it, because I don’t. And nothing out there fills the void, so far. infrastructure. So please, don’t be polite, but criticize. Especially, if you find it (1) The Haskell Platform is the common ground for all Haskellers useful. and we give a summarizing overview of its ingredients. (2) Cabal *** CONTINUE HERE *** libraries.tex 2009-02-14, 2011-10-27 haskell doc September 6, 2012 2 The Standard Haskell Environment Each of the mentioned applications comes with a built–in man- ual, which shows when you add --help to the commands name.
    [Show full text]