关于列表和其他东西在Clojure的问题 [英] Questions about Lists and other stuff in Clojure

查看:79
本文介绍了关于列表和其他东西在Clojure的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些关于clojure中的列表,类和变量的问题。


  1. 这可能看起来很蠢,但是如何访问元素在列表中?

我编写的程序允许您操纵电话簿;您可以添加条目,删除一个条目或打印有关条目的信息。这有两个问题:


  1. 有一种方法来创建一个类entry包含名称地址电话号码变量?或者在clojure中是不可能的(和函数式编程一般?)如果我不能有包含该信息的对象列表,我该如何去完成这个任务?


  2. 我想有一个函数,读取用户输入,以了解用户想要做什么(添加条目,删除条目或打印信息)然后调用相应的函数做回调第一个函数完成后。是作为参数传递给每个函数的条目列表正确的事情做什么?



解决方案


如何访问列表中的元素?




 (nth coll index)

例如:

 (nth [1 2 3 4] 2); - > 3(因为它使用基于零的索引)




创建一个类条目,将包含名称地址电话号码变量?或者是不可能在clojure(和函数式编程一般?)如果我不能有一个包含该信息的对象列表,我该如何去执行这个任务?


这是可能在Clojure,但unidiomatic。在Clojure中,数据实体的基本抽象是地图,而不是类(除了需要与Java框架直接互操作的一些角落情况)。所以你只需要使用一个地图:

 (def entry {:namex:addressy:phone-number z})

要访问该项目的名称,您可以使用

 (:name entry)

 (get entry:name)

前者仅在地图的关键字是关键字时使用,后者适用于所有类型的关键字。



您的示例中,您的数据模型(电话簿)将是这样的地图的seq(例如,列表或向量)。


想到有一个函数读取用户输入,以了解用户想要做什么(添加条目,删除条目或打印信息),然后调用相应的函数来完成调用第一个函数。


由于您的模型只包含一个主要数据结构(电话本seq),作为一个arg传递它当然是一个适当的方式来设计你的函数。如果您希望有更多种类的顶级容器(例如,对于更现实的应用程序),我建议您查看应用程序上下文模式,这将看起来有点令人害怕起初(至少它为我做了,它包含很多Clojure特定的术语),但很好值得努力学习。


I have a few questions concerning Lists, classes and variables in clojure.

  1. This may seem quite stupid but how do I access elements in a List ?

I'm coding a program that allows you to manipulate a phonebook; You can add an entry, delete one or print the information about one. This leads me to two questions :

  1. Is there a way to create a class "entry" that would contain "name" "adress" "phone number" variables ? or is that impossible in clojure (and functional programming in general ?) If I can't have a List of objects containing that information, how would I go about this task ?

  2. I was thinking of having a function that reads user input to know what the user wants to do (add entry, delete entry or print info) then calls the appropriate function to do that which calls back the first function when it's done. Is passing as a parameter the List of entries to each function the right thing to do ?

解决方案

This may seem quite stupid but how do I access elements in a List ?

(nth coll index)

For example:

(nth [1 2 3 4] 2) ; -> 3 (since it uses zero-based indexing)

Is there a way to create a class "entry" that would contain "name" "adress" "phone number" variables ? or is that impossible in clojure (and functional programming in general ?) If I can't have a List of objects containing that information, how would I go about this task ?

It's possible in Clojure, but unidiomatic. In Clojure, the basic abstraction for data entities are maps, not classes (except for some corner cases where direct interoperation with Java frameworks is needed). So you would just use a map:

(def entry {:name "x" :address "y" :phone-number "z"})

To access the item's name, you either use

(:name entry)

or

(get entry :name)

The former works only when the keys of the map are keywords, the latter works with all types of key.

So for your example, your data model (the phonebook) would be a seq (say, a list or a vector) of such maps.

I was thinking of having a function that reads user input to know what the user wants to do (add entry, delete entry or print info) then calls the appropriate function to do that which calls back the first function when it's done. Is passing as a parameter the List of entries to each function the right thing to do?

Since your model consists of only one main data structure (the phone book seq), passing it as an arg is certainly an appropriate way to design your functions. If you expect to have more kinds of top-level containers (i.e. for a more real world application), I'd recommend looking into the Application Context Pattern, which will look a bit intimidating at first (at least it did for me, and it contains a lot of Clojure-specific jargon), but is well worth the effort to learn.

这篇关于关于列表和其他东西在Clojure的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆