def foo():
x = 5
def bar():
print x
def baz():
x = 6
print x
bar()
baz()
print x
Which prints 5, 6, 5. If baz tried to print x before assigning, it would be an UnboundLocalError. fix f = f (fix f)
This is probably the clearest definition you could ask for of what a fixed-point combinator actually is (other than something like "fix f = f (f (f (f (f ...", which is more difficult to give to a computer). fix f = let x = f x in x
(Of course, due to type-checking, it's non-trivial to define the actual non-recursive Y in Haskell; you usually have to resort to using some type-level recursion. But in an untyped non-strict language you could define it easily enough.) 1. map (*2) [1..10]
2. sum [1..1000]
-- sum = foldl (+) 0
3. any (`isInfixOf` tweet) wordlist
4. fileText <- readFile "data.txt"
fileLines <- lines <$> readFile "data.txt"
5. mapM_ putStrLn . map (\i -> "Happy Birthday " ++ if i == 3 then "dear NAME" else "to You") $ [1..4]
6. (passed, failed) = partition (>60) [49,58,76,82,88,90]
7. -- I avoid XML so I don't know what library you'd use here.
8. minimum [14, 35, -7, 46, 98]
-- minimum = foldl1 min