Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Aprende Haskell en 10 minutos
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Definici贸n de [[Function|funciones]] == Hab铆amos escrito la definici贸n de [[Introduction to Haskell IO/Actions|acciones IO]], llamada <hask>main</hask>: <haskell> main = do putStrLn "What is 2 + 2?" x <- readLn if x == 4 then putStrLn "You're right!" else putStrLn "You're wrong!" </haskell> Ahora podemos suplementar escribiendo la definici贸n de una ''[[function|funci贸n]]'' y llam谩ndola <hask>factorial</hask>. Tambi茅n agregar茅 un modulo de cabecera. <haskell> module Main where factorial n = if n == 0 then 1 else n * factorial (n - 1) main = do putStrLn "What is 5! ?" x <- readLn if x == factorial 5 then putStrLn "You're right!" else putStrLn "You're wrong!" </haskell> Construyalo otra vez usando <tt>ghc --make Test.hs</tt>. y, $ ./Test What is 5! ? 120 You're right! Ah铆 tenemos una funci贸n. Tal como las que vienen por defecto, se puede llamar como <hask>factorial 5</hask> sin par茅ntesis. Podemos preguntar el [[type|tipo]] a <tt>ghci</tt>. $ ghci Test.hs << GHCi banner >> Ok, modules loaded: Main. Prelude Main> :t factorial factorial :: (Num a) => a -> a Los tipos de las funciones se escriben como el tipo del los argumentos, seguidos de <hask> -> </hask>, y luego el tipo del resultado. (Este ejemplo tambi茅n tiene la clase de tipo <hask>Num</hask>). El factorial se puede simplificar escribiendo el an谩lisis de casos. <haskell> factorial 0 = 1 factorial n = n * factorial (n - 1) </haskell>
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width