|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://angg.twu.net/HASKELL/Comprehensions1.hs.html
-- http://angg.twu.net/HASKELL/Comprehensions1.hs
-- (find-angg "HASKELL/Comprehensions1.hs")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- Notes on the chapter "5. List Comprehensions" of Hutton's book.
-- (find-books "__comp/__comp.el" "haskell-hutton")
-- (find-books "__comp/__comp.el" "haskell-hutton" "List comprehensions")
-- (find-es "haskell" "list-comprehensions")
-- (find-angg "HASKELL/DoNotation1.hs")
--
-- (defun hc () (interactive) (find-angg "HASKELL/Comprehensions1.hs"))
import Control.Monad
{-
* (eepitch-ghci)
* (eepitch-kill)
* (eepitch-ghci)
:load Comprehensions1.hs
[x^2 | x <- [1..5]]
[(x,y) | x <- [1,2,3], y <- [4,5]]
[(x,y) | y <- [4,5], x <- [1,2,3]]
[(x,y) | x <- [1..3], y <- [x..3]]
-}
concat :: [[a]] -> [a]
concat xss = [x | xs <- xss, x <- xs]
firsts :: [(a,b)] -> [a]
firsts ps = [x | (x,_) <- ps]
-- length :: [a] -> Int
-- length xs = sum [1 | _ <- xs]
factors :: Int -> [Int]
factors n = [x | x <- [1..n], n `mod` x == 0]
prime :: Int -> Bool
prime n = factors n == [1,n]
primes :: Int -> [Int]
primes n = [x | x <- [2..n], prime x]
find :: Eq a => a -> [(a,b)] -> [b]
find k t = [v | (k',v) <- t, k == k']
pairs :: [a] -> [(a,a)]
pairs xs = zip xs (tail xs)
sorted :: Ord a => [a] -> Bool
sorted xs = and [x <= y | (x,y) <- pairs xs]
positions :: Eq a => a -> [a] -> [Int]
positions x xs = [i | (x',i) <- zip xs [0..], x == x']
lowers :: String -> Int
lowers xs = length [x | x <- xs, x >= 'a' && x <= 'z']
count :: Char -> String -> Int
count x xs = length [x' | x' <- xs, x == x']
{-
* (eepitch-ghci)
* (eepitch-kill)
* (eepitch-ghci)
:load Comprehensions1.hs
factors 15
factors 7
prime 15
prime 7
primes 40
find 'b' [('a',1),('b',2),('c',3),('b',4)]
zip ['a','b','c'] [1,2,3,4]
pairs [1,2,3,4]
sorted [1,2,3,4]
sorted [1,3,2,4]
positions False [True, False, True, False]
"abcde" !! 2
take 3 "abcde"
length "abcde"
zip "abc" [1,2,3,4]
lowers "Haskell"
count 's' "Mississippi"
-}
-- TODO:
-- (find-books "__comp/__comp.el" "haskell-hutton")
-- (find-books "__comp/__comp.el" "haskell-hutton" "The Caesar cipher")
-- Local Variables:
-- coding: utf-8-unix
-- End: