Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by FoxtrotUniform (Prior)
on Jun 23, 2004 at 20:56 UTC ( [id://369174]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    Pm_tut> :t negate
    negate :: Num a => a -> a
    
  2. or download this
    Pm_tut> :t (-)
    (-) :: Num a => a -> a -> a
    
  3. or download this
    nats = [0..]
    
  4. or download this
    Pm_tut> take 10 nats
    [0,1,2,3,4,5,6,7,8,9]
    
  5. or download this
    open FH, '<', $file or die "Can't open $file for reading: $!\n";
    
  6. or download this
    const x y = x
    
  7. or download this
    (-) :: Num a => a -> a -> a
    
  8. or download this
    (-) :: Num a => (a, a) -> a
    
  9. or download this
    (-) :: Num a => a -> (a -> a)
    
  10. or download this
    (5-)
    
  11. or download this
    Pm_tut> putStr "Hello, world!\n"
    Hello, world!
    
    Pm_tut>
    
  12. or download this
    factorial n = if n == 0 then 1
                    else n * factorial (n-1)
    
  13. or download this
    Pm_tut> :t factorial
    factorial :: Num a => a -> a
    
  14. or download this
    factorial 0 = 1
    factorial n = n * factorial (n-1)
    
  15. or download this
    factorial n = n * factorial (n-1)
    factorial 0 = 1
    
  16. or download this
    factorial 0     = 1
    factorial (n+1) = (n+1) * factorial n
    
  17. or download this
    factorial n = tr n 1 where
        tr 0 f = f
        tr n f = tr (n-1) (n*f)
    
  18. or download this
    factorial n = product [1..n]
    
  19. or download this
    product = foldl (*) 1
    
  20. or download this
    foldl f z []     =  z
    foldl f z (x:xs) =  foldl f (f z x) xs
    
  21. or download this
    product xs = foldl (*) 1 xs
    
  22. or download this
    sum xs = foldl (+) 0 xs        -- this is in the Standard Prelude, too
    
  23. or download this
    or xs = foldl (||) False xs   -- this is also in the Prelude
    
  24. or download this
    and xs = foldl (&&) True xs    -- so is this
    
  25. or download this
    anyprime xs = or (map prime xs)  -- map does what you'd expect
    allprime xs = and (map prime xs)
    
  26. or download this
    foldl :: (a -> b -> a) -> a -> [b] -> a
    
  27. or download this
    andprime p n = p && prime n
    allprime xs  = foldl andprime True xs
    
  28. or download this
    allprime xs = foldl (\p n -> p && prime n) True xs
    
  29. or download this
    factorials = scanl (*) 1 [1..]
    
  30. or download this
    Pm_tut> take 10 factorials
    [1,1,2,6,24,120,720,5040,40320,362880]
    Pm_tut> factorials !! 24
    620448401733239439360000
    
  31. or download this
    Pm_tut> factorials !! 65536
    
    ERROR - Garbage collection fails to reclaim sufficient space
    Pm_tut>
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found