Definitions
اسلاید 1: Definitions
اسلاید 2: name :: Typeanswer :: Int name = expressionanswer = 12+13Definitions associate a namewith a value of a certain typeis of typegreater :: Boolgreater = (answer > 56)newline :: Char newline = ‘n’yes :: Boolyes = True
اسلاید 3: Expressions and evaluation
اسلاید 4: __ __ __ __ ____ ___ __________________________________ || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard ||___|| ||__|| ||__|| __|| 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 functionsEvaluating 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):
اسلاید 5: 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
اسلاید 6: hello, worldPrelude> putStr Hello, world Hello, worldPrelude> Hello ++ , ++ world Hello, worldPrelude> sum [1..)ERROR: Syntax error in expression (unexpected `))Prelude> sum aERROR: Type error in application *** expression : sum a *** term : a *** type : Char *** does not match : [a]Prelude> sum [1..n]ERROR: Undefined variable nPrelude> Prelude> putStr hello, >> putStr world
اسلاید 7: User Defined Functions
اسلاید 8: Defining functionsfact :: Int -> Intfact n = product [1..n] fact is of type Int to IntfunctionnameType of argumentType of result resultargumentImportant: 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.
اسلاید 9: exOr :: Bool -> Bool -> Bool exOr a b = (a || b) && not (a && b)Examplessquare :: Int -> Int square n = n * nallEqual :: Int -> Int -> Int -> Bool allEqual m n p = (n == m) && (n == p) maxi :: Int -> Int -> Int maxi m n | n >= m = n | otherwise = m
اسلاید 10: Working with functionsPrelude> 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
اسلاید 11: 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=0comb :: 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 -}
اسلاید 12: Two styles-- myFirst.hs-- Haskell is fun -- function to raise an integer to the power of 2.squ :: Int -> Intsqu n = n * nLiterate StyleFirstLit.lhsHaskell is fun function to raise an integer to the power of 2.> squ :: Int -> Int> squ n = n * n
اسلاید 13: 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 followOpen an existing file or create one using an editorSave the file (use only .hs or .lhs extensions depending the on the style)Launch HugsLoad (reload ) the file you have savedTest your functions edit the file to correct any errorsSave the editted file and repeat from step ivWorking with Scripts
اسلاید 14: Getting help from Hugs
اسلاید 15: 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:
اسلاید 16: SummaryWe have learned how toDefine functionsConstruct expressions using the functions we define and built-in functionsEvaluate expressions (similar to the way the numeric expressions are evaluated in a calculator.)
نقد و بررسی ها
هیچ نظری برای این پاورپوینت نوشته نشده است.