DOC PREVIEW
U of I CS 421 - Lecture notes

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

The Hask LanguageCS421 Lecture 11: Hask1Mark [email protected] of Illinois at Urbana-ChampaignJune 26, 20061Based on slides by Mattox Beckman, as updated by Vikram Adve, GulAgha, and Elsa GunterMark Hills CS421 Lecture 11: HaskThe Hask LanguagePutting Concepts to PracticeAt this poi nt, we have a good idea of what syntax is, and of whatlexemes, or tokens, in the language are. We now want to put thisto use i n taking a programming language and figuring out whichtokens are used in any given program – or figuring out that there isa lexical error in the source co de. Our language will b eMark Hills CS421 Lecture 11: HaskThe Hask LanguagePutting Concepts to PracticeAt this poi nt, we have a good idea of what syntax is, and of whatlexemes, or tokens, in the language are. We now want to put thisto use i n taking a programming language and figuring out whichtokens are used in any given program – or figuring out that there isa lexical error in the source co de. Our language will b eHask(a shorter Haskell...)Mark Hills CS421 Lecture 11: HaskThe Hask LanguageHaskHask is a (partially) lazy functional language that is a subset of thelanguage Haskell. The concepts are similar to what we have lookedat so far for OCaml, with some significant differences that shouldmake it interesting.Mark Hills CS421 Lecture 11: HaskThe Hask LanguageSimple ExpressionsA variety of simple expressions are available in the language. Thisincludes arithmetic expressions, relational expressions, and logicalexpressions.Mark Hills CS421 Lecture 11: HaskThe Hask LanguageArithmetic Expressions1 1 + 2 ;; -- 32 3 - 1 ;; -- 23 4 * 3 ;; -- 124 4 / 2 ;; -- 25 4 ** 2 ;; -- 16Mark Hills CS421 Lecture 11: HaskThe Hask LanguageLogical Expressions1 1 < 2 ;; -- True2 1 > 2 ;; -- False3 1 == 2 ;; -- False4 1 /= 2 ;; -- TrueMark Hills CS421 Lecture 11: HaskThe Hask LanguageBoolean Expressions1 True && False ;; -- False2 True || False ;; -- True3 not True ;; -- FalseMark Hills CS421 Lecture 11: HaskThe Hask LanguageVariable BindingsTop-level variable bindings do not require a let (although we haveone, which we’ll see in a bit):1 x = 5 ;;2 y = 6 ;;3 x + y ;; -- 11Mark Hills CS421 Lecture 11: HaskThe Hask LanguageFunctionsFunctions are the core model of computation in Hask, much like inOCaml. One difference is that we will treat function parameterslazily – instead of evaluating them first (often called eager or strictevaluation), we will wait to evaluate them until we find a use ofthem in the function body.Mark Hills CS421 Lecture 11: HaskThe Hask LanguageFunctions1 [- Sum two numbers -]2 fun sum x y = x + y ;;34 [- Double a given number -]5 fun double x = 2 * x ;;67 [- Composition -- double sum result -]8 fun sum_then_double = sum . double ;;Mark Hills CS421 Lecture 11: HaskThe Hask LanguageAnonymous Functions1 [- Anonymous version of sum -]2 (\ x y -> x + y) 2 3 ;; -- 534 [- Application function -]5 (\ f x -> f x) (\ x -> x + 1) 3 ;; -- 4Mark Hills CS421 Lecture 11: HaskThe Hask LanguageType ExpressionsWe can also use type expressions to say what the expected type ofa function is . We have type inference, but this provides go oddocumentation of our i ntentions.1 [- Sum two numbers -]2 sum :: Int -> Int -> Int ;;3 fun sum x y = x + y ;;45 [- Double a given number -]6 double :: Int -> Int ;;7 fun double x = 2 * x ;;89 [- Composition -- double sum result -]10 sum_then_double :: Int -> Int -> Int ;;11 fun sum_then_double = sum . double ;;Mark Hills CS421 Lecture 11: HaskThe Hask LanguageLet BindingsLocal bindings come in two forms. The first is the let, which isfamiliar from OCaml, although we always have an in portion ofthe let.1 [- Single binding -]2 let x = 5 in x ** x ;; -- 312534 [- More complex, with multiple bindings -]5 let f = (\ x -> x + x) ; g = 3 in f g ;; -- 6Mark Hills CS421 Lecture 11: HaskThe Hask LanguageWhere BindingsThe second form of binding is the where binding, which provides amethod of giving l ocal bindings in a function. The main differenceis that t he where portion comes after the function body. Notethat and and end aren’t used in Haskell, but are here to make ourlife easier.1 fun sum_double x y = double (sum x y)2 where fun sum a b = a + b3 and fun double a = 2 * a4 end5 ;;67 sum_double 2 3 ;; -- 10Mark Hills CS421 Lecture 11: HaskThe Hask LanguageConditionalsWe also have a conditional expression, which always must haveboth branches.1 if True then 5 else 6 ;; -- 523 (if False then (\ x -> x + 1)4 else (\ x -> x + 2)) 10 ;; -- 12Mark Hills CS421 Lecture 11: HaskThe Hask LanguageListsList have similar syntax to OCaml, but with some slight differences.1 [1,2,3] ;; -- [1,2,3]23 1 : [2,3] ;; -- [1,2,3]45 [1,2] ++ [3] ;; -- [1,2,3]67 [1 .. 10] ;; -- [1,2,3,4,5,6,7,8,9,10]The latter i s a list comprehension – we will only use the simpleform shown here, although much more complex forms are allowed.Mark Hills CS421 Lecture 11: HaskThe Hask LanguageTuplesTuples are also similar to those in OCaml, using the same syntax.We will restrict our use to pairs, since this lets us have predefinedoperations to get the values back out.1 (1,2) ;; -- pair of integers23 fst (1,2) ;; -- 145 snd (1,2) ;; -- 267 snd ((\ x -> x), (\ y -> y)) ;; -- \ y -> yMark Hills CS421 Lecture 11: HaskThe Hask LanguageCommentsYou’ve probably noticed two comment forms used in the aboveexamples.◮-- is a line comment, and wil l comment out anything throughthe end of the l ine◮[− b egi ns a block command, with −] then ending thecomment; block comments can be nestedMark Hills CS421 Lecture 11: HaskThe Hask LanguagePicky Details◮All characters will be from the ASCII character set◮Lowercase letters are all letters from a to z◮Uppercase letters are all letters from A to Z◮Digits are all numbers from 0 to 9◮Whitespace includes spaces, tabs, and newlines◮Identifiers for data items (variables, functions) all start with alowercase letter, and then consist of 0 or more lowercaseletters, upp ercase letters, digi ts, underscores, and singlequotes◮Identifiers for types are defined identically to those for dataitems except they start with an uppercase letterMark Hills CS421 Lecture 11: HaskThe Hask LanguagePicky Details, cont.◮Numbers are all integers, made up of one or more digits,potentially with a leading sign◮Strings are surrounded with double quotes and can includeany character typeable on a standard keyboard, plus standardescape characters, indicated with a preceeding backslash◮Parens can be used to surround


View Full Document

U of I CS 421 - Lecture notes

Documents in this Course
Lecture 2

Lecture 2

12 pages

Exams

Exams

20 pages

Lecture

Lecture

32 pages

Lecture

Lecture

21 pages

Lecture

Lecture

15 pages

Lecture

Lecture

4 pages

Lecture

Lecture

68 pages

Lecture

Lecture

68 pages

Lecture

Lecture

84 pages

s

s

32 pages

Parsing

Parsing

52 pages

Lecture 2

Lecture 2

45 pages

Midterm

Midterm

13 pages

LECTURE

LECTURE

10 pages

Lecture

Lecture

5 pages

Lecture

Lecture

39 pages

Load more
Download Lecture notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?