Commit a49fbacd authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.

Initial commit

parents
## Example files for the title:
# Real World Haskell, by Bryan O'Sullivan
[![Real World Haskell, by Bryan O'Sullivan](http://akamaicovers.oreilly.com/images/9780596514983/cat.gif)](https://www.safaribooksonline.com/library/view/title/9780596155339//)
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.
--# char
'c'
:type 'c'
--# string
"a string literal"
:type "a string literal"
--# stringlist
['a', ' ', 's', 't', 'r', 'i', 'n', 'g'] == "a string"
--# empty
"\&"
"foo\&bar"
--# empty.example
"\130\&11"
import Data.List
{-- snippet minima --}
-- lines beginning with "--" are comments.
minima k xs = take k (sort xs)
{-- /snippet minima --}
{-- snippet main --}
-- lines beginning with "--" are comments.
main = interact wordCount
where wordCount input = show (length (lines input)) ++ "\n"
{-- /snippet main --}
--# arithmetic
2 + 2
31337 * 101
7.0 / 2.0
--# bignum
313 ^ 15
--# neg.simple
-3
--# neg.error
2 + -3
--# neg.better
2 + (-3)
3 + (-(13 * 37))
--# whitespace.ok
2*3
--# whitespace.eek
2*-3
--# whitespace.whew
2*(-3)
--# pi
pi
--# e
e
--# let
let e = exp 1
--# epipi
(e ** pi) - pi
--# boolean
True && False
False || True
--# boolean.bad
True && 1
--# comparison
1 == 1
2 < 3
4 >= 3.99
--# neq
2 /= 3
--# not
not True
--# parens
1 + (4 * 4)
1 + 4 * 4
--# precedence
:info (+)
:info (*)
--# grouping
5 * 8 / 3
5 * (8 / 3)
--# module
:module + Data.Ratio
--# denom.bad
22 % 7 * 7 % 22
--# denom.good
(22 % 7) * (7 % 22)
--# intpower
12 ^ 2
12.1 ^ 2
--# exponent.bad
12 ^ 2.1
--# exponent.zero
12 ^ 0
12 ^ (-1)
--# exponent.neg
12 ^^ (-2)
0 ^^ (-2)
--# exponent.float
12.2 ** 2.1
--# concat
[3,1,3] ++ [3,7]
[] ++ [False,True] ++ [True]
--# cons
1 : [2,3]
1 : []
--# string
"This is a string."
--# newline
putStrLn "Here's a newline -->\n<-- See?"
--# list
[1, 2, 3]
--# list.shortlong
[]
["foo", "bar", "baz", "quux", "fnord", "xyzzy"]
--# list.bad
[True, False, "testing"]
--# range
[1..10]
--# range.step
[1.0,1.25..2.0]
[1,4..15]
[10,9..1]
--# char
'a'
--# range.char
['a','e'..'z']
--# work
let a = ['l', 'o', 't', 's', ' ', 'o', 'f', ' ', 'w', 'o', 'r', 'k']
a
a == "lots of work"
--# emptystring
"" == []
--# newstring
'a':"bc"
"foo" ++ "bar"
--# range.double
[1.0..1.8]
--# prefix
2 + 2
(+) 2 2
--# caret
:info (^)
--# set_t_on
:set +t
'c'
"foo"
--# it
"foo"
it ++ "bar"
--# it.bad
it
it ++ 3
it
it ++ "baz"
--# integer
7 ^ 80
--# ratio
:m +Data.Ratio
11 % 29
--# ratio.bad
3.14 % 8
1.2 % 3.4
--# set_t_off
:unset +t
2
--# type
:type 'a'
"foo"
:type it
--# different
3 + 2
:type it
:type 3 + 2
{-- snippet assign --}
x = 10
x = 11
{-- /snippet assign --}
{-- snippet odd --}
isOdd n = mod n 2 == 1
{-- /snippet odd --}
{-- snippet type --}
take :: Int -> ([a] -> [a])
{-- /snippet type --}
take = undefined
--# load
:load Assign
## snippet assign
x = 10
x = 11
# value of x is now 11
print x
## /snippet assign
--# error
True && "false"
--# types
:type 'a'
'a' :: Char
[1,2,3] :: Int
--# readFile
:type readFile
--# head
head [1,2,3,4]
head ['a','b','c']
--# tail
tail [1,2,3,4]
tail [2,3,4]
tail [True,False]
tail "list"
tail []
--# takeDrop
take 2 [1,2,3,4,5]
drop 3 [1,2,3,4,5]
--# lines.type
:type lines
--# lines
lines "the quick\nbrown fox\njumps"
--# take.type
:type take
--# headDrop
head (drop 4 "azerty")
--# last
last [1,2,3,4,5]
last "baz"
--# last.type
:type last
--# not.type
:type not
--# fst.type
:type fst
--# odd
odd 3
odd 6
--# compare
compare 2 3
compare 3 3
compare 3 2
--# precedence
(compare 2 3) == LT
compare 2 3 == LT
--# precedence2
compare (sqrt 3) (sqrt 6)
--# precedence3
compare sqrt 3 sqrt 6
--# drop
drop 2 "foobar"
drop 4 "foobar"
drop 4 [1,2]
drop 0 [1,2]
drop 7 []
drop (-2) "foo"
--# myDrop
:load myDrop.hs
myDrop 2 "foobar"
myDrop 4 "foobar"
myDrop 4 [1,2]
myDrop 0 [1,2]
myDrop 7 []
myDrop (-2) "foo"
--# myDrop1
:type 2 <= 0 || null "abcd"
--# myDrop2
2 <= 0
--# myDrop2a
:type False || null "abcd"
--# myDrop3
null "abcd"
--# myDrop4
False || False
--# myDrop5
:type (2 - 1) <= 0 || null (tail "abcd")
--# myDrop6
:type (2 - 1) <= 0
2 - 1
1 <= 0
--# myDrop7
:type null (tail "abcd")
tail "abcd"
null "bcd"
--# myDrop8
False || False
--# myDrop9
:type (1 - 1) <= 0 || null (tail "bcd")
--# myDrop10
:type (1 - 1) <= 0
1 - 1
0 <= 0
--# myDrop11
True || null (tail "bcd")
--# myDrop12
:type tail "bcd"
--# myDrop13
myDrop (1 - 1) (tail "bcd") == tail "bcd"
--# myDrop14
myDrop (2 - 1) (tail "abcd") == tail "bcd"
--# myDrop15
myDrop 2 "abcd" == tail "bcd"
--# myDrop16
myDrop 2 "abcd"
tail "bcd"
--# null
:type null
:type (||)
--# print
print (myDrop 2 "abcd")
{-- snippet myDrop.type --}
myDrop :: Int -> [a] -> [a]
{-- /snippet myDrop.type --}
{-- snippet myDrop --}
myDrop n xs = if n <= 0 || null xs
then xs
else myDrop (n - 1) (tail xs)
{-- /snippet myDrop --}
{-
{-- snippet myDrop1 --}
myDrop n xs = if n <= 0 || null xs
then xs
else myDrop (n - 1) (tail xs)
{-- /snippet myDrop1 --}
-}
{-- snippet myDrop2 --}
myDropX n xs = if n <= 0 || null xs then xs else myDropX (n - 1) (tail xs)
{-- /snippet myDrop2 --}
{-- snippet niceDrop --}
niceDrop n xs | n <= 0 = xs
niceDrop _ [] = []
niceDrop n (_:xs) = niceDrop (n - 1) xs
{-- /snippet niceDrop --}
## snippet myDrop
def myDrop(n, elts):
while n > 0 and elts:
n = n - 1
elts = elts[1:]
return elts
## /snippet myDrop
--# shadow
let x = 1
let f n = x + n
x
let x = "foo"