صفحه 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.)