GHCi: Getting started (1A) Young Won Lim 5/26/17

Similar documents
GHCi: Getting started (1A) Young Won Lim 5/31/17

GHCi: Getting started (1A) Young Won Lim 6/3/17

Libraries (1A) Young Won Lim 6/5/17

Haskell Overview III (3A) Young Won Lim 10/4/16

Haskell Overview IV (4A) Young Won Lim 10/13/16

Polymorphism Overview (1A) Young Won Lim 2/20/18

Haskell Overview IV (4A) Young Won Lim 10/24/16

Monad (1A) Young Won Lim 6/9/17

Side Effects (3A) Young Won Lim 1/13/18

Monad (1A) Young Won Lim 6/21/17

Standard prelude. Appendix A. A.1 Classes

Haskell Types COMP360

Haskell Overview II (2A) Young Won Lim 8/9/16

Background Type Classes (1B) Young Won Lim 6/14/18

Monad Background (3A) Young Won Lim 11/20/17

Background Expressions (1D) Young Won Lim 7/7/18

Monad (1A) Young Won Lim 6/26/17

Side Effects (3B) Young Won Lim 11/23/17

Background Operators (1E) Young Won Lim 7/7/18

Monad Background (3A) Young Won Lim 11/8/17

Side Effects (3B) Young Won Lim 11/20/17

Side Effects (3B) Young Won Lim 11/27/17

Applicatives Comparisons (2C) Young Won Lim 3/6/18

State Monad (3D) Young Won Lim 9/25/17

Monad Background (3A) Young Won Lim 11/18/17

Background Type Classes (1B) Young Won Lim 6/28/18

Monad Background (3A) Young Won Lim 10/5/17

Haskell Overview II (2A) Young Won Lim 8/23/16

Monad (3A) Young Won Lim 8/9/17

State Monad (3D) Young Won Lim 8/31/17

Overview (1A) Young Won Lim 9/14/17

Overview (1A) Young Won Lim 9/9/17

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Algorithms (7A) Young Won Lim 4/10/17

Haskell Overview II (2A) Young Won Lim 9/26/16

State Monad Example (3H) Young Won Lim 2/16/18

Function Overview (1A) Young Won Lim 10/23/17

Overview (1A) Young Won Lim 9/25/17

Logic Haskell Exercises

Background Constructors (1A) Young Won Lim 7/9/18

Maybe Monad (3B) Young Won Lim 12/21/17

CS 320: Concepts of Programming Languages

CSc 520. Principles of Programming Languages 11: Haskell Basics

Background Constructors (1A) Young Won Lim 1/22/18

A Sudoku Solver (1A) Richard Bird Implementation. Young Won Lim 11/15/16

Pointers (1A) Young Won Lim 1/9/18

Pointers (1A) Young Won Lim 1/5/18

Programming Paradigms

Background Constructors (1A) Young Won Lim 7/5/18

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

IO Monad (3C) Young Won Lim 8/23/17

Pointers (1A) Young Won Lim 11/1/17

Functions (4A) Young Won Lim 5/8/17

Maybe Monad (3B) Young Won Lim 1/3/18

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

Background Constructors (1A) Young Won Lim 6/30/18

Accessibility (1A) Young Won Lim 8/22/13

Algorithms (7A) Young Won Lim 4/18/17

Monad P1 : Several Monad Types (4A) Young Won Lim 2/13/19

Applications of Pointers (1A) Young Won Lim 12/26/17

Functions (4A) Young Won Lim 3/16/18

Monoid (4A) Young Won Lim 8/12/17

State Monad Examples (6C) Young Won Lim 9/26/18

Pointers (1A) Young Won Lim 3/5/18

Background Functions (1C) Young Won Lim 4/3/18

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

Applications of Arrays (1A) Young Won Lim 2/11/17

CS 360: Programming Languages Lecture 10: Introduction to Haskell

State Monad Examples (6C) Young Won Lim 9/22/18

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

Operators (2A) Young Won Lim 10/2/13

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Lecture 1 August 9, 2017

IO Monad (3D) Young Won Lim 1/18/18

Haskell through HUGS THE BASICS

IO Monad (3C) Young Won Lim 1/6/18

Programming in Haskell Aug Nov 2015

Applications of Arrays (1A) Young Won Lim 3/15/17

Arrays (1A) Young Won Lim 1/27/17

Pointers (1A) Young Won Lim 1/14/18

Introduction to Haskell

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

CS 11 Haskell track: lecture 1

Algorithms Overview (1A) Young Won Lim 3/29/18

Background Functions (1C) Young Won Lim 5/26/18

Background Functions (1C) Young Won Lim 12/6/17

Arrays (1A) Young Won Lim 12/4/17

Binary Search Tree (3A) Young Won Lim 6/2/18

Algorithms Bubble Sort (1B) Young Won Lim 4/5/18

A Sudoku Solver Pruning (3A)

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

CSc 372 Comparative Programming Languages

HW / SW Implementation Overview (0A) Young Won Lim 7/16/16

Binary Search Tree (2A) Young Won Lim 5/17/18

Programming Languages Fall 2013

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Box-and-arrow Diagrams

Transcription:

GHCi: Getting started (1A)

Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Please send corrections (or suggestions) to youngwlim@hotmail.com. This document was produced by using OpenOffice.

Based on Haskell in 5 steps https://wiki.haskell.org/haskell_in_5_steps 3

Interpreter GHCi young@mntsys-bb1 ~ $ ghci GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help Prelude> "hello, world!" "hello, world!" Prelude> putstrln "hello, world!" hello, world! 4

Function Prelude> let fac n = if n == 0 then 1 else n * fac (n-1) Prelude> fac 5 120 Prelude> fac 2 2 Prelude> fac 3 6 Prelude> fac 4 24 Prelude> 5

Compiler GHC young@mntsys-bb1 ~ $ ghc -o hello hello.hs [1 of 1] Compiling Main ( hello.hs, hello.o ) Linking hello... young@mntsys-bb1 ~ $./hello hello, world! young@mntsys-bb1 ~ $ cat hello.hs main = putstrln "hello, world!" 6

Layout t.hs main = do putstrln "Type an integer :?" x <- readln if even x then putstrln "even number" else putstrln "odd number" the first non-space character after do. every line that starts in the same column as that p is in the do block If you indent more, it is the nested block in do If you indent less, it is an end of the do block. ghc t.hs./t Ghc o run t.hs./t 7

Multi-line in GHCi ghci multi-line Prelude> :{ Prelude main = do { putstrln "Type an integer: "; x<-readln; Prelude if even x then putstrln "even" else putstrln "odd"; } Prelude :} 8

Types Int Integer Float Double Rational an integer with at least 30 bits of precision. an integer with unlimited precision. a single precision floating point number. a double precision floating point number. a fraction type, with no rounding error. Types and Class Types start with capital letters Variables start with lower case letters Declaring a type Asking which type :: type :t something 9

Type Classes Prelude> 3 :: Int 3 Prelude> 3 :: Float 3.0 Prelude> 4 :: Double 4.0 Prelude> 2 :: Integer 2 Prelude> :t 3 3 :: Num a => a 3 can be used as any numeric type Prelude> :t 2.0 2.0 :: Fractional a => a 2.0 can be used as any fractional type Prelude> :t gcd 15 20 gcd 15 20 :: Integral a => a Prelude> :t True True :: Bool Prelude> :t 'A' 'A' :: Char class constraint (Num t) => (Fractional t) => (Integral t) => gcd 15 20 can be used as any integral type the type t is constrained by the context (Num t), (Fractional t), (Integral t) the types of t must be Num type class the types of t must be Fractional type class the types of t must be Integral type class 10

Type Classes Int Integer Float Double Rational Instances of Integral type Instances of Fractional type Instances of Num type Type Class : a set of type (instances) Instances of Num type Int Integer Float Double Rational Instances of Integral type Instances of Fractional type 11

Lists and Tuples Lists multiple values of the same type Strings lists of characters. Tuples a fixed number of values, which can have different types. The : operator appends an item to the beginning of a list Zip : two lists into a list of tuples. 12

Functions [1.. 10] map (+ 2) [1.. 10] filter (> 2) [1.. 10] [1,2,3,4,5,6,7,8,9,10] [3,4,5,6,7,8,9,10,11,12] [3,4,5,6,7,8,9,10] fst (1, 2) snd (1, 2) map fst [(1, 2), (3, 4), (5, 6)] 1 2 [1,3,5] fst (1, 2, 3) snd (1, 2, 3) 13

Functions my_sum m n = m+n main = do putstrln "Give two numbers: " x <- readln y <- readln print (my_sum x y) Give two numbers: 10 20 30 14

Convenient Syntax secstoweeks secs =let perminute = 60 perhour = 60 * perminute perday = 24 * perhour perweek = 7 * perday in secs / perweek classify age = case age of 0 -> "newborn" 1 -> "infant" 2 -> "toddler" _ -> "senior citizen" 15

Using Libraries import Prelude hiding (lookup) import Data.Map employeedept = fromlist([("john","sales"), ("Bob","IT")]) deptcountry = fromlist([("it","usa"), ("Sales","France")]) countrycurrency = fromlist([("usa", "Dollar"), ("France", "Euro")]) employeecurrency :: String -> Maybe String employeecurrency name = do dept <- lookup name employeedept country <- lookup dept deptcountry lookup country countrycurrency main = do putstrln $ "John's currency: " ++ (show (employeecurrency "John")) putstrln $ "Pete's currency: " ++ (show (employeecurrency "Pete")) https://downloads.haskell.org/~ghc/latest/docs/html/libraries/containers-0.5.7.1/data-map-lazy.html 16

References [1] ftp://ftp.geoinfo.tuwien.ac.at/navratil/haskelltutorial.pdf [2] https://www.umiacs.umd.edu/~hal/docs/daume02yaht.pdf