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.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Operators, Functions and Modules1 Operators and Functions.
Chapter 10.
CMT Programming Software Applications
0 PROGRAMMING IN HASKELL Chapter 2 - First Steps.
Advanced Programming Andrew Black and Tim Sheard Lecture 2 Intro to Haskell.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Data types and variables
Chapter 2: Introduction to C++.
Introduction to C Programming
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Objectives You should be able to describe: Data Types
Introduction to Python
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Python Basic Syntax. Basic Syntax - First Program 1 All python files will have extension.py put the following source code in a test.py file. print "Hello,
Operators, Functions and Modules1 Pattern Matching & Recursion.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Linux Operations and Administration
CS465 - UNIX The Bourne Shell.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Overview of the Haskell 98 Programming Language
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Linux+ Guide to Linux Certification, Second Edition
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Guards1. 2 Let’s test this function. Main> maxi Here is a trace of the function: n m maxi 3 2 Guards or conditions are used to express various cases.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Linux Administration Working with the BASH Shell.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
C++ First Steps.
Programming Languages
Chapter 2 - Introduction to C Programming
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
Basic operations in Matlab
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Programming Languages
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
15-110: Principles of Computing
PROGRAMMING IN HASKELL
Chapter 2 - Introduction to C Programming
12th Computer Science – Unit 5
PROGRAMMING IN HASKELL
Introduction to C Programming
Presentation transcript:

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.)