### RFC: A Perlesque Introduction to Haskell, Part One (DRAFT)

by FoxtrotUniform (Prior)
 on Jun 23, 2004 at 20:56 UTC Need Help??

```Pm_tut> :t negate
negate :: Num a => a -> a
```
```Pm_tut> :t (-)
(-) :: Num a => a -> a -> a
```
```nats = [0..]
```
```Pm_tut> take 10 nats
[0,1,2,3,4,5,6,7,8,9]
```
```open FH, '<', \$file or die "Can't open \$file for reading: \$!\n";
```
```const x y = x
```
```(-) :: Num a => a -> a -> a
```
```(-) :: Num a => (a, a) -> a
```
```(-) :: Num a => a -> (a -> a)
```
```(5-)
```
```Pm_tut> putStr "Hello, world!\n"
Hello, world!

Pm_tut>
```
```factorial n = if n == 0 then 1
else n * factorial (n-1)
```
```Pm_tut> :t factorial
factorial :: Num a => a -> a
```
```factorial 0 = 1
factorial n = n * factorial (n-1)
```
```factorial n = n * factorial (n-1)
factorial 0 = 1
```
```factorial 0     = 1
factorial (n+1) = (n+1) * factorial n
```
```factorial n = tr n 1 where
tr 0 f = f
tr n f = tr (n-1) (n*f)
```
```factorial n = product [1..n]
```
```product = foldl (*) 1
```
```foldl f z []     =  z
foldl f z (x:xs) =  foldl f (f z x) xs
```
```product xs = foldl (*) 1 xs
```
```sum xs = foldl (+) 0 xs        -- this is in the Standard Prelude, too
```
```or xs = foldl (||) False xs   -- this is also in the Prelude
```
```and xs = foldl (&&) True xs    -- so is this
```
```anyprime xs = or (map prime xs)  -- map does what you'd expect
allprime xs = and (map prime xs)
```
```foldl :: (a -> b -> a) -> a -> [b] -> a
```
```andprime p n = p && prime n
allprime xs  = foldl andprime True xs
```
```allprime xs = foldl (\p n -> p && prime n) True xs
```
```factorials = scanl (*) 1 [1..]
```
```Pm_tut> take 10 factorials
[1,1,2,6,24,120,720,5040,40320,362880]
Pm_tut> factorials !! 24
620448401733239439360000
```
```Pm_tut> factorials !! 65536

ERROR - Garbage collection fails to reclaim sufficient space
Pm_tut>
```

Create A New User
Node Status?
node history
Node Type: perlmeditation [id://369174]
Front-paged by Enlil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
As of 2020-02-23 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What numbers are you going to focus on primarily in 2020?

Results (102 votes). Check out past polls.

Notices?