Advertisement
STANAANDREY

functs list

Feb 17th, 2025
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type List<A> = Nil | Cons<A>
  2.  
  3. interface Nil {
  4.     _tag: 'Nil'
  5. }
  6. interface Cons<A> {
  7.     _tag: 'Cons',
  8.     head: A,
  9.     tail: List<A>,
  10. }
  11.  
  12. const nil: List<never> = { _tag: 'Nil' }
  13. const cons = <A>(head: A, tail: List<A>):List<A> => ({
  14.     _tag: 'Cons',
  15.     head, tail,
  16. })
  17.  
  18. const isNil = <A>(xs: List<A>): xs is Nil => xs._tag === 'Nil'
  19.  
  20. const myList = cons(1, cons(2, cons(3, nil)))
  21. console.log(JSON.stringify(myList, null, 2))
  22.  
  23. type ShowList = <A>(xs: List<A>) => string
  24. const showList: ShowList = xs => isNil(xs) ? '' : `${xs.head}, ${showList(xs.tail)}`
  25.  
  26. console.log(showList(myList))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement