Definitions
name :: Type answer :: Int name = expression answer = Definitions associate a name with a value of a certain type is of type greater :: Bool greater = (answer > 56) newline :: Char newline = ‘\n’ yes :: Bool yes = True
Expressions and evaluation
__ __ __ __ ____ ___ __________________________________ || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard ||___|| ||__|| ||__|| __|| Copyright (c) ||---|| ___|| World Wide Web: || || Report bugs to: || || 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 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 banner something like the following (1):
40 Prelude> sum [1..10] 55 Prelude> 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
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> Prelude> putStr "hello, " >> putStr "world"
User Defined Functions
Defining functions fact :: Int -> Int fact n = product [1..n] fact is of type Int to Int function name Type of argument Type of result result argument 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.
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
Working with functions Prelude> square 5 25 Prelude> allEqual True Prelude> allEqual False Prelude> maxi Prelude> maxi Prelude> exOr True False True Prelude> exOr True True False Prelude> exOr True (not False) False
Scripts A script looks like what you saw on slide 16. It contains definitions (definitions of functions and other values) as well as comments. Another Script 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)!) Comments are preceeded by - - or enclosed in braces: -- this is a comment {- this is also a comment -}
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
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.Launch Hugs iv.Load (reload ) the file you have saved v.Test your functions vi.edit the file to correct any errors vii.Save the editted file and repeat from step iv Working with Scripts
Getting help from Hugs
Prelude> :? LIST OF COMMANDS: Any command may be abbreviated to :c where c is the first character in the full name. :load load modules from specified files :l myFirst.hs :load clear all files except prelude :also read additional modules :reload repeat last load command :project use project file :edit edit file :e myFirst.hs :edit edit last module :e :module set module for evaluating expressions evaluate expression :type print type of expression :? display this list of commands :set set command line options :set help on command line options :names [pat] list names currently in scope :info describe named objects :browse browse names defined in :find 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:
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.)