Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a...

16
Definitions

Transcript of Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a...

Page 1: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

Definitions

Page 2: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

name :: Type

answer :: Int

name = expression

answer = 12+13

Definitions associate a namewith a value of a certain type

is of type

greater :: Bool

greater = (answer > 56)

newline :: Char

newline = ‘\n’

yes :: Bool

yes = True

Page 3: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

Expressions and evaluation

Page 4: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

__ __ __ __ ____ ___ __________________________________ || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard ||___|| ||__|| ||__|| __|| Copyright (c) 1994-1999 ||---|| ___|| World Wide Web: http://haskell.org/hugs || || Report bugs to: [email protected] || || Version: September 1999 _________________________________

Haskell 98 mode:Restart with command line option -98 to enable extensions  Reading file "/Hugs/lib/Prelude.hs": Hugs session for: /Hugs/lib/Prelude.hs Type :? for help Prelude>Prelude is a special module that contains definitions for built-in functions

Evaluating expressionsTo begin with, we have to start the Hugs interpreter; the way to do this is by using the command hugs, which produces a startup banner something like the following (1):

Page 5: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

40Prelude> sum [1..10] 55 Prelude> 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 55 Prelude> (not True) || False False Prelude> reverse "Hugs is cool" "looc si sguH" Prelude> filter even [1..10] [2, 4, 6, 8, 10] Prelude> take 10 fibs where fibs = 0:1:zipWith (+) fibs (tail fibs) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Prelude> (2+3)*8

Page 6: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

hello, worldPrelude> putStr "Hello, world" Hello, worldPrelude> "Hello" ++ ", " ++ "world" "Hello, world"Prelude> sum [1..)ERROR: Syntax error in expression (unexpected `)')Prelude> sum 'a'ERROR: Type error in application *** expression : sum 'a' *** term : 'a' *** type : Char *** does not match : [a]Prelude> sum [1..n]ERROR: Undefined variable "n"Prelude>

Prelude> putStr "hello, " >> putStr "world"

Page 7: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

User Defined Functions

Page 8: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

Defining functions

fact :: Int -> Intfact n = product [1..n] fact is of type Int to Int

functionname

Type of argument

Type of result

resultargument

Important: A name starts with a letter and is followed by a sequence of letters, digits, underscore and single quotes. There are some reserved words like case, do and if which can not be used as identifiers. Only types start with a capital letter.

Page 9: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

exOr :: Bool -> Bool -> Bool exOr a b = (a || b) && not (a && b)

Examples

square :: Int -> Int

square n = n * n

allEqual :: Int -> Int -> Int -> Bool

allEqual m n p = (n == m) && (n == p) maxi :: Int -> Int -> Int maxi m n | n >= m = n | otherwise = m

Page 10: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

Working with functions

Prelude> square 525Prelude> allEqual 8 8 8TruePrelude> allEqual 8 4 8FalsePrelude> maxi 7 37Prelude> maxi 3 03Prelude> exOr True FalseTrue Prelude> exOr True TrueFalsePrelude> exOr True (not False)False

Page 11: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

ScriptsA script looks like what you saw on slide 16. It contains definitions (definitions of functions and other values) as well as comments.

Another Scriptfact :: Int –> Int fact n

|n = = 0 =1|n>0 =fact (n-1)*n|otherwise =0

comb :: Integer -> Integer -> Integercomb n r = fact n `div` (fact r * fact (n - r))The number of different ways of selecting r objects from a collection of n objects using the formula n!/(r!(n-r)!)

Comments are preceeded by - - or enclosed in braces:

-- this is a comment

{- this is also a comment -}

Page 12: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

Two styles

-- myFirst.hs

-- Haskell is fun

-- function to raise an integer to the power of 2.

squ :: Int -> Int

squ n = n * n

Literate Style

FirstLit.lhs

Haskell is fun

function to raise an integer to the power of 2.

> squ :: Int -> Int

> squ n = n * n

Page 13: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

An expression entered at the prompt may not be longer than a line.

You can not define functions at the “prelude>” prompt. To work with a script you have to use an editor. You have to put your scripts into files and load them when you

want to use them. You may use editors like Notepad or Wordpad to create the file

and edit it. Steps to followi. Open an existing file or create one using an editorii. Save the file (use only .hs or .lhs extensions depending the on the style)

iii. Launch Hugsiv. Load (reload ) the file you have savedv. Test your functions vi. edit the file to correct any errorsvii. Save the editted file and repeat from step iv

Working with Scripts

Page 14: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

Getting help from Hugs

Page 15: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

  Prelude> :? LIST OF COMMANDS: Any command may be abbreviated to :c where c is the first character in the full name.  :load <filenames> load modules from specified files :l myFirst.hs :load clear all files except prelude :also <filenames> read additional modules :reload repeat last load command :project <filename> use project file :edit <filename> edit file :e myFirst.hs :edit edit last module :e :module <module> set module for evaluating expressions <expr> evaluate expression :type <expr> print type of expression :? display this list of commands :set <options> set command line options :set help on command line options :names [pat] list names currently in scope :info <names> describe named objects  :browse <modules> browse names defined in <modules> :find <name> edit module containing definition of name :!command shell escape :cd dir change directory :gc force garbage collection :version print Hugs version :quit exit Hugs interpreter :q Prelude>

The :? command displays the following summary of all Hugs commands:

Page 16: Definitions. name :: Type answer :: Int name = expression answer = 12+13 Definitions associate a name with a value of a certain type is of type greater.

Summary

We have learned how to

Define functions

Construct expressions using the functions we define and built-in

functions

Evaluate expressions (similar to the way the numeric expressions

are evaluated in a calculator.)