صفحه 1:
‘Definitions
صفحه 2:
is of بح
type Definitions associate a nam
with a value of a certain ty]
answer :: Int
answer = 12+13
newline :: Char
newline = ‘\n’
yes Bool
yes = True
greater :: Bool
greater = (answer > 56)
صفحه 3:
Expressions and evaluation
صفحه 4:
Evaluating expressions
To begin with, we have to start the Hugs
interpreter;
the way to do this is by using the
command hugs, which produces a startup
han ner lsometh 1۳۵۲0۲۲6۷۲۱30 )1(۰
100-00 (ها اس اما الحا
مايه ساديدا f =I Lj ae
Il Report bap to! huebane@kehel.ory,
9 امه تسود || |
موی ای و 0 تون سا اج موی لت تنعل 96 ماما
(Reactor Pile "/ Ware /ib/Pretrde.bs":
يونا مه Was
Mawel! Prekrde be
‘Dupe 2? Por key
یب
یسوط مس سا و سپ پوس bs Soe ایب پم (Sig pte pe
صفحه 5:
(مبم) مس
0
Prekde> suv [0.00]
ss
Prete? 1+ O+9+F+64+047404+9400
وه
Prekrle> (aot له || (ص
Gree
(ee eter eters
یا
[0.0] مس بط Preknle>
]9, 6,6, 9, [
Prekrle> the OO Bibs where Phe = O:0:2ip(Dik (+) Bibs (tal Pos)
[O, 4, 4, 6, 9, 9, 6, 65, G4, SF]
صفحه 6:
rekide> puGir "kelly, ۲ << ی انم
kell, word
Prokide> ان "Wels, work"
Welle, wort
rekde> “Well” ++") "++ "World"
“Welly, world"
مه حطس ]0.(
6080008: م6 error ia expression (uaexpevied *)')
@rekide> suc 'o!
CGRROR: Pye error ماو و
oxpressiva 1 معد
و
عد + Olek
** does ant watch | [ul]
.]مه حطس
لول :03000ع) vartuble "a"
Prehide>
صفحه 7:
User Defined Functions
صفحه 8:
Defining functions
> eS
fact 33 Int 5
fact n = product [1..n]
fact is of
type Int t
Important:
Y Aname starts with a letter and is followed by a sequence of letters,
digits, underscore and single quotes.
Y There are some reserved words like case, do and if which can not be
used as identifiers.
Y Only types start with a capital letter.
صفحه 9:
Examples
square :: Int -> Int
squaren=n*n
allEqual :: Int -> Int -> Int->Bool
allEqual m n p = (n==m) &&(n
maxi :: Int -> Int -> Int
maxi mn
| << =n
| otherwise =m
exOr :: Bool -> Bool -> Bool
exOr a b =(a || b) && not(a
&& b)
صفحه 10:
Working with functions
@rekxe> SQuare 5
25
@retee> allEqual 8 8 8
True
allEqual 8 4 8 مم
False
@rete> Maxi 7 3
2
0 3 2281 یه
9
ات
True
@rette> EXOr True True
False
@retse> EXOr True (not False)
False
صفحه 11:
ts
‘ipt looks like what you saw on slide 16. It contains definitic
nitions of functions and other values) aS well as comments.
¥ Comments are preceeded by -- or enclosed in
braces:
-- this is a comment
Arfotthis alse a comment -}
fact :: Int -> Int
fact n
jn== =1
|n>0 =fact (n-1)*n
jotherwise =0
comb :: Integer -> Integer -> Integer
comb nr = fact n ‘div’ (fact r * fact (n - r))
ane number of different ways 90 electing r objects from a
collection of n objects using the f ormula n!/ 0 -r)!)
صفحه 12:
۲۷0 5
-- 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
صفحه 13:
with Scripts ای
An expression entered at the prompt may not be
longer than a line.
7” You can not define functions at the “prelude>”
prompt.
” To work with a script you have to use an editor.
Y You have to put your scripts into files and load them
when you want to use them.
Y You may use editors like Notepad or Wordpad to
create the file and edit it.
Steps to follow
i. Open an existing file or create one using an editor
ii. Save the file (use only -hs or .Ihs extensions depending the
on the style)
iii. Launch Hugs
iv. Load (reload ) the file you have saved
v. Test your functions
vi. edit the file to correct any errors
xni Save the aditted file and reneat from ctean iv
صفحه 14:
Getting help from Hugs
صفحه 15:
تمه تیا له اه وه ر سل با مرو ای 7: Whe
و یمه
te whore و اما be بو امه بو :00۵00۵۵۵۵۵ ۵6 ۱/۵۱
عض لبخ i hor جوا ابو tthe
toad سا اما ما سفن لصا هلاه a
shod clear ol Pls enrept prone
sta Herne ماه اس stiles
۳ اجان
0ك
ایحا > ادن
cet elt ket crank
whe <arnhle> set wonkle و اه و
<exr> وه لد
uve <em> موی سر
7 | oP crocs
"et Sopane> ام ال
‘met |
0
واه ام سا Ino owes?
browne <anhdes> browee camer dePrard #1 Sorbie?
جک تب eck come eric مد once
مسا hel escape
ide coker ceo
ie Pore carbene order
levees prt Lane versio
موز ent Deng وه
یج
صفحه 16:
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.)
Definitions
is of
type
name
::
Type
Definitions associate a nam
with a value of a certain typ
answer :: Int
name
=
answer = 12+13
newline :: Char
newline = ‘\n’
yes :: Bool
yes = True
greater :: Bool
greater = (answer > 56)
expression
Expressions and evaluation
Evaluating expressions
To begin with, we have to start the Hugs
interpreter;
the way to do this is by using the
command
hugs,__________________________________
which produces a startup
__ __ __ __ ____ ___
|| || || || || || ||__
Hugs 98: Basedlike
on the Haskell
98
standard
banner
something
the
following
(1):
||___|| ||__|| ||__|| __||
Copyright (c) 1994-1999
||---||
___||
World Wide Web: http://haskell.org/hugs
|| ||
Report bugs to: hugs-bugs@haskell.org
|| || 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
Prelude> (2+3)*8
40
Prelude> 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> putStr "hello, " >> putStr "world"
hello, world
Prelude> putStr "Hello, world"
Hello, world
Prelude> "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>
User Defined Functions
Defining functions
functio
n
name
fact
fact
Type of
argum
ent
::
Int
->
n = product [1..n]
argum
type Int to Int
ent
Important:
result
Type
of
result
Int
fact is of
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.
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
exOr :: Bool -> Bool -> Bool
exOr a b = (a || b) && not (a
&& b)
Working with functions
Prelude>
square 5
25
Prelude> allEqual
8 8 8
Prelude> allEqual
8 4 8
True
False
Prelude> maxi
7
Prelude> maxi
3
Prelude> exOr
7 3
Prelude> exOr
True True
Prelude> exOr
True (not False)
True
False
False
3 0
True False
ts
ript looks like what you saw on slide 16. It contains definitio
finitions of functions and other values) as well as comments.
Comments are preceeded by - - or enclosed in
braces:
-- this is a comment
{- this Script
is also a comment -}
Another
fact :: Int –> Int
fact n
|n = = 0
=1
|n>0
=fact (n-1)*n
|otherwise
=0
comb :: Integer -> Integer -> Integer
comb 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)!)
Two styles
-- myFirst.hs
-- Haskell is fun
-- function to raise an integer to the power of 2.
squ :: Int
squ
n
->
=
Int
n *
n
Literate Style
FirstLit.lhs
Haskell is fun
function to raise an integer to the power of 2.
> squ ::
> squ
Int
->
n=n * n
Int
Working with Scripts
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 follow
i. Open an existing file or create one using an editor
ii. Save the file (use only .hs or .lhs extensions depending the
on the style)
iii.
iv.
v.
vi.
Launch Hugs
Load (reload ) the file you have saved
Test your functions
edit the file to correct any errors
Getting help from Hugs
The :? command displays the following summary of all Hugs commands:
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
: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
:edit
edit last module
: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
Prelude>
:l myFirst.hs
:e myFirst.hs
:e
:q
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.)