Giter VIP home page Giter VIP logo

Comments (3)

sjames1958gm avatar sjames1958gm commented on May 28, 2024

Oops now I think I see, notFound is a binding in the match not a matching?

from purescript-jordans-reference.

JordanMartinez avatar JordanMartinez commented on May 28, 2024

Correct. notFound is a binding that binds to the third argument passed to findFirst'. Since I'm not pattern matching on the value, it can be either Nothing or Just a. The example below that section shows that it is initially Nothing and then becomes Just 1. Here's another graph reduction if we passed in an empty list:

findFirst Nil (\el -> el == 1)
findFirst' Nil (\el -> el == 1) Nothing
Nothing

And to make things more explicit:

findFirst :: forall a. List a -> (a -> Boolean) -> Maybe a
-- Match 1
findFirst list condition = findFirst' list condition Nothing

findFirst' :: forall a. List a -> (a -> Boolean) -> Maybe a -> Maybe a
-- Match 1
findFirst' Nil condition notFound = notFound
-- Match 2
findFirst' (Cons head tail) condition theA@(Just alreadyFound) =
  findFirst' tail condition theA
-- Match 3
findFirst' (Cons head tail) condition Nothing =
  let foundOrNot = if (condition head) then (Just head) else Nothing
  in findFirst' tail condition foundOrNot
findFirst (Cons 0 (Cons 1 (Cons 2 Nil))) (\el -> el == 1)
-- findFirst - Match 1: match found. Replace args with that match's body

findFirst' (Cons 0 (Cons 1 (Cons 2 Nil))) (\el -> el == 1) Nothing
-- findFirst' - Match 1: match failed. 1st arg was `Cons`, not a `Nil`. Go to next match
-- findFirst' - Match 2: match failed. 3rd arg was `Nothing`, not a `Just`. Go to next match
-- findFirst' - Match 3: match found. Replace args with that match's body.

findFirst'         (Cons 1 (Cons 2 Nil))  (\el -> el == 1) Nothing
-- findFirst' - Match 1: match failed. 1st arg was `Cons`, not a `Nil`. Go to next match
-- findFirst' - Match 2: match failed. 3rd arg was `Nothing`, not a `Just`. Go to next match
-- findFirst' - Match 3: match found. Replace args with that match's body.

findFirst'                 (Cons 2 Nil)   (\el -> el == 1) (Just 1)
-- findFirst' - Match 1: match failed. 1st arg was `Cons`, not a `Nil`. Go to next match
-- findFirst' - Match 2: match found. Replace args with that match's body.

findFirst'                         Nil    (\el -> el == 1) (Just 1)
-- findFirst' - Match 1: match found. Replace args with that match's body.

Just 1

from purescript-jordans-reference.

sjames1958gm avatar sjames1958gm commented on May 28, 2024

Thanks - I understand it better now, thanks for the detailed response.

from purescript-jordans-reference.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.