Com S 541 Lecture -*- Outline -*- * Introduction to Haskell ** interaction (Thompson chapter 2, replaces Davie 2.1) see also the on-line documentation for Hugs ------------------------------------------ INTERACTING WITH THE INTERPRETER $ hugs __ __ __ __ ____ ___ || || || || || || ||__ ||___|| ||__|| ||__|| __|| ||---|| ___|| || || || || Version: Nov 2003 Haskell 98 mode: ... Type :? for help Prelude> ------------------------------------------ ... hugs ... :quit ------------------------------------------ MORE INTERACTION Prelude> Prelude> :type 1 1 :: Num a => a Prelude> 3 + 4 7 :: Int Prelude> (+) 3 4 7 :: Int Prelude> 216 * 34 7344 :: Int Prelude> 7344 / 34 216.0 :: Double Prelude> 7 / 3 2.33333 :: Double Prelude> div 7 3 2 :: Int Prelude> 7 `div` 3 2 :: Int Prelude> 7 `rem` 3 1 :: Int Prelude> 7 `divRem` 3 ERROR: Undefined variable "divRem" Prelude> 7 `quotRem` 3 (2,1) :: (Int,Int) Prelude> :type 7 `quotRem` 3 quotRem 7 3 :: Integral a => (a,a) ------------------------------------------ ... ? 1 1 :: Int (Unlike Davie, you just end expressions with a newline, they can only be on one line.) Another correction, p. 13, use quotRem instead of divRem See handout and the manual page for more details ------------------------------------------ WORKING WITH FILES Prelude> :load "Fact.hs" Fact> fact fact {dict} {dict} :: Int -> Int Fact> :type fact fact :: (Num a, Enum a) => a -> a Fact> fact 4 24 Fact> fact 100 0 Fact> :edit Fact.hs Fact> :reload Fact> :type fact fact :: Integer -> Integer Fact> fact 100 9332621544394415268169923885626670049... Fact> Fact> :q [Leaving Hugs] ------------------------------------------ for the first version of Fact.hs, use > fact n = product [1 .. n] it works to say fact (toInteger 100), when used in the interpreter, showing the result makes the literal acquire type Int the :reload command reloads the file you're working on, if you don't want to invoke the editor from inside Hugs. use setenv EDITOR vi to change the default editor to vi ------------------------------------------ popeye 15% hugs Fact.hs [...] Reading file "... Prelude.hs" Reading file "Fact.hs": Type :? for help Fact> fact 8 40320 Fact> :q [Leaving Hugs] ------------------------------------------ ------------------------------------------ LITERATE SCRIPTS file foo.lhs: ============================== A TITLE Some text, whatever you want.. > -- part of a Haskell prog > hd (x:_) = x The blank lines surrounding the program are mandatory. This file should have a .lhs suffix, or use the +l argument to the interpreter. ============================== Prelude> :load "foo.lhs" Main> hd (3 : (4 : [])) 3 :: Int ------------------------------------------ program parts have > as first character on line, so even this file can be processed as a program! use hugs +l introduction.txt