Need help with syntax

Hey everyone, need little help to make a recursive function with pattern matching. So question is like:-
Suppose a function takes to input (Integer and List[Integer]) and gives back an integer
So let input be :- [1,2,3,4,5,6] and 4
And the desired output is [1,2,3]
2.)
Input:- [42,32,77,46,34,56] and 46
Output :- [42,32,77]

2 Likes

I believe you want to use takeWhile, which is documented here.

A couple of examples matching the ones you made:

takeWhile (/= 4) [1, 2, 3, 4, 5, 6]
takeWhile (/= 46) [42, 32, 77, 46, 34, 56]

A convenient way to look for functions in the DAML standard librarys is using Hoogle at daml.com, where you can look for functions by their signature, as in this case in which I looked for a function with the signature (a -> Boolean) -> [a] -> [a].

4 Likes

If you want a function that returns the initial segment up to, but excluding that element, you can make @stefanobaghino-da’s construction generic and define:

takeUntil x = takeWhile (/= x)
3 Likes