1 / 21

Functional Programming 1 Haskell Basics

Functional Programming 1 Haskell Basics. Dr. Ahmed Sallam. Reference. Main: Introduction to Haskell By Brent Yorgey Further. Functional Programming. Function is the atom of the language, and can be used exactly as any other sort of value.

mira
Download Presentation

Functional Programming 1 Haskell Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Haskell Basics Functional Programming1 Haskell Basics Dr. Ahmed Sallam

  2. Reference • Main: Introduction to HaskellBy Brent Yorgey • Further Haskell Basics

  3. Functional Programming • Function is the atom of the language, and can be used exactly as any other sort of value. • Programming style is centered around evaluating expressions rather than executing instructions. Haskell Basics

  4. Haskell • Named after logician Haskell Curry. • It was created in the late 1980 by a committee of academics where everyone had his favorite functional language. • It combines some of the best ideas from existing languages, and a few new ideas. Haskell Basics

  5. Haskell Characteristics • Functional • Pure • Lazy • Statically typed Haskell Basics

  6. Haskell Characteristics • Pure • Immutable declarations • Referential transparency Haskell Basics

  7. Haskell Characteristics • Pure • No side effects: (e.g. updating global variables) • Parallelism: evaluating expressions in parallel. • Lazy • Expressions are not evaluated until their results are needed. • Statically Typed • Each expression has a fixed type. (expression with errors will not compiled) Haskell Basics

  8. Basic concepts • Types • Haskell’s rich of types. • Clarify thinking in the program structure • Serve as documentation • Turns run-time errors into compile-time errors. • Abstraction • “Don’t repeat yourself” • Polymorphism • Higher order functions Haskell Basics

  9. Basic concepts • Wholemeanl programming • first solve a more general problem, then extract the interesting bits and pieces by transforming the general program into more specialised ones. Haskell Basics

  10. Declarations and Variables • Get in the mode Haskell Basics

  11. Basic Types • Int : +/- 2^n (Calculate n as following) • Integer: limited by your machine memory. • Float, Double: real numbers. • Bool: Booleans. • Char: Unicode Characters. • String Haskell Basics

  12. GHCI Tips • :load or :l is used to load .hs file. • :reload or :r is used to reload .hs file • :type or :t is used to ask for expression type • :? Is used to show help (show available commands) Haskell Basics

  13. Arithmetic • Note: mod operation • Note: negative numbers Grave Accent Haskell Basics

  14. Arithmetic • Casting must be explicit • fromIntegral : ineger to all • Round, floor, ceiling • Use (div) instead of (/) with integers Haskell Basics

  15. Logic • Comparisons: ( < , >, <=, >=, =, /=) • Combine : ( ||, && , not () ) Haskell Basics

  16. Logic • Else is mandatory Haskell Basics

  17. Function basic definition Haskell Basics

  18. Using function • Functions has the highest priority. • sum 5 n+1 3 parse as (sum 5 n) + (1 3) • If you want to pass n+1 as argument , write • sum 5 (n+1) 3 Haskell Basics

  19. Lists • a = 1 : [] • b = 3 : (1 : []) • c = [2,3,4] == 2 : 3 : 4 : [] Haskell Basics

  20. Function on lists • We can replace h with _ Haskell Basics

  21. Useful links • Haskell Portal • Haskell Platform Haskell Basics

More Related