1 / 13

Understanding the Haskell `take` Function: How Functional Programs Work

This article delves into the Haskell programming language's built-in `take` function, which serves as a fundamental example of functional programming concepts. The `take` function extracts a specified number of elements from an infinite list, demonstrating the lazy evaluation characteristic of Haskell. We explore the execution steps of `take` through an infinite list, breaking down the recursive definition of the function and showing how it processes inputs. By analyzing the example `take 3 [1..]`, we gain insights into the inner workings of functional programs in Haskell.

dacey
Télécharger la présentation

Understanding the Haskell `take` Function: How Functional Programs Work

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. Formal Models of Computation Running Haskell Programs - take

  2. How functional programs work… take 0 xs = [] take (n+1) (x:xs) = x:(take n xs) Main> take 3 [1..] [1,2,3] • Comments: • [1..] is an infinite data structure • take is built-in in Haskell • If you want to try the definition above, change its name to myTake or something like that… formal models of computation

  3. How functional programs work… Execution: take 0 xs = [] take (n+1) (x:xs) = x:(take n xs) take 3[1..] • 3matches(n+1)assigning 2 to n • The infinite list [1..] matches (x:xs), assigning 1 to x and [2..] (aha!) to xs formal models of computation

  4. How functional programs work… Execution: take 0 xs = [] take (n+1) (x:xs) = x:(take n xs) take 3 [1..]  1:(take 2 [2..]) formal models of computation

  5. How functional programs work… Execution: take 0 xs = [] take (n+1) (x:xs) = x:(take n xs) take 3 [1..]  1:(take 2 [2..]) formal models of computation

  6. How functional programs work… Execution: take 0 xs = [] take (n+1) (x:xs) = x:(take n xs) take 3 [1..]  1:(take 2 [2..]) • 2matches(n+1)assigning 1 to n • The infinite list [2..] matches (x:xs), assigning 2 to x and [3..] to xs formal models of computation

  7. How functional programs work… Execution: take 0 xs = [] take (n+1) (x:xs) = x:(take n xs) take 3 [1..]  1:(2:(take 1 [3..])) formal models of computation

  8. How functional programs work… Execution: take 0 xs = [] take (n+1) (x:xs) = x:(take n xs) take 3 [1..]  1:(2:(take 1 [3..])) formal models of computation

  9. How functional programs work… Execution: take 0 xs = [] take (n+1) (x:xs) = x:(take n xs) take 3 [1..]  1:(2:(take 1 [3..])) • 1matches(n+1)assigning 0 to n • The infinite list [3..] matches (x:xs), assigning 3 to x and [4..] to xs formal models of computation

  10. How functional programs work… Execution: take 0 xs = [] take (n+1) (x:xs) = x:(take n xs) take 3 [1..]  1:(2:(3:(take 0 [4..]))) formal models of computation

  11. How functional programs work… Execution: take 0 xs = [] take (n+1) (x:xs) = x:(take n xs) take 3 [1..]  1:(2:(3:(take 0 [4..]))) formal models of computation

  12. How functional programs work… Execution: take 0 xs = [] take (n+1) (x:xs) = x:(take n xs) take 3 [1..]  1:(2:(3:[]))) formal models of computation

  13. How functional programs work… Execution: take 0 xs = [] take (n+1) (x:xs) = x:(take n xs) take 3 [1..]  [1,2,3] formal models of computation

More Related